Good evening, given given, how to get the next select after a given select if the select id is unknown? I need, depending on the choice of the first select, to activate the second select. So I tried, based on the comment and the answer, it does not work ...

<script> function SetFilter(id){ alert(document.getElementById(id).value); if (document.getElementById(id).value!='-') $("#"+id).nextAll("div select:first").attr('disabled',''); else $("#"+id).nextAll("div select:first").attr('disabled','disabled'); } </script> <div> <p class="flabel">ะœะฐั€ะบะฐ</p> <select name="PROPERTY_CML2_MARKKA" id="PROPERTY_CML2_MARKKA" onchange="SetFilter(id)"> <option value="-">-</option> <option value="audi">audi</option> <option value="audi2">audi2</option> <option value="ford">ford</option> </select> </div> <div> <p class="flabel">ะœะพะดะตะปัŒ</p> <select name="PROPERTY_CML2_MODEL" id="PROPERTY_CML2_MODEL" onchange="SetFilter(id)"> <option value="-">-</option><option value="audi 5">audi 5</option> <option value="ford model1">ford model1</option> <option value="model2">model2</option> </select> </div> <div> <p class="flabel">ะ“ะพะด ะฒั‹ะฟัƒัะบะฐ</p> <select name="PROPERTY_CML2_YEAR" id="PROPERTY_CML2_YEAR" onchange="SetFilter(id)"> <option value="-">-</option> <option value="1999">1999</option> <option value="2006">2006</option> </select> </div> 

Is something missing? I need the next select to be active / inactive

  • @ Jean-Claude, updated html code - NNN
  • @ Jean-Claude, it still does not work - NNN

2 answers 2

Well, if so:

Javascript:

 function SetFilter(id){ // ะขะตะบัƒั‰ะธะน ัะตะปะตะบั‚ var el = $('#' + id); // ะ˜ะดะตะฝั‚ะธั„ะธะบะฐั‚ะพั€ ัะปะตะดัƒัŽั‰ะธะตะณะพ ะฝัƒะถะฝะพะณะพ ัะตะปะตะบั‚ะฐ var next = el.attr('data-next-item'); if (next) { if (el.val() !== '-') { $("#" + next).removeAttr('disabled'); } else { $("#" + next).attr('disabled', 'disabled'); } } } 

HTML:

 <div> <p class="flabel">ะœะฐั€ะบะฐ</p> <select data-next-item="PROPERTY_CML2_MODEL" id="PROPERTY_CML2_MARKKA" onChange="SetFilter(id)"> <option value="-">-</option> <option value="audi">audi</option> <option value="audi2">audi2</option> <option value="ford">ford</option> </select> </div> <div> <p class="flabel">ะœะพะดะตะปัŒ</p> <select data-next-item="PROPERTY_CML2_YEAR" id="PROPERTY_CML2_MODEL" disabled onchange="SetFilter(id)"> <option value="-">-</option><option value="audi 5">audi 5</option> <option value="ford model1">ford model1</option> <option value="model2">model2</option> </select> </div> <div> <p class="flabel">ะ“ะพะด ะฒั‹ะฟัƒัะบะฐ</p> <select id="PROPERTY_CML2_YEAR" disabled onchange="SetFilter(id)"> <option value="-">-</option> <option value="1999">1999</option> <option value="2006">2006</option> </select> </div> 

Essence: you specify at what will be the following, and you process.

Living example:

http://codepen.io/bustexz/pen/yOXGqv

  • data-next-item - an attribute that you come up with? I didnโ€™t think that attributes can be invented .... - NNN
  • one
    possible =) But not entirely simple, all attributes must begin with data- . Here are the docks htmlbook.ru/samouchitel-html5/atributy-data - Vasily Barbashev
  • You can also replace the data-next-item attribute with the name , it works. Thank you for the invaluable knowledge! - NNN
  • one
    Everything is possible, just to store meta-data, it's better to use your attributes. The name attribute is still reserved, and its logic is the name of the current element, not the next one. Everything should have its own logic) - Vasily Barbashev

Depends, of course, on the DOM structure.

Here are some recipes:

  1. If the elements follow each other strictly, use next(ัะตะปะตะบั‚) .

  2. If they follow each other, but are separated by other nextAll(ัะตะปะตะบั‚:first) elements, then try nextAll(ัะตะปะตะบั‚:first) - it will show the first element found. If you need some kind of non-first, then you can like this: $(nextAll(ัะตะปะตะบั‚)->get(ะฝะพะผะตั€)) .

  3. You can also use sibling selectors .

PS Wrote from memory without checking, file it.

  • I tried it your way, it does not work - ((( - NNN