Tutorial :How do I select an html-select jquery



Question:

My html is such:

<select id="slctDiv2" class="slct"><option value="0">Any</option>  <option   value="1">Administration</option><option value="2">Collections</option>       <option value="3">Distribution</option><option value="4">Engineering</option>  <option value="5">Treatment</option></select>  

How do I get the ID of the select item (in this case "slctDiv2") if I found the selected option via jquery.

$('#slctDiv1,#slctDiv2').change(function(){      if($(this).parent().attr('id')=='slctDiv2'){      //do this     }else{      //do that    }  });  

Using the parent function doesn't work.


Solution:1

I believe you already have the select (according to the ID in the HTML and the IDs you have in the JS), not the option, so you only need to do:

$('#slctDiv1,#slctDiv2').change(function(){      if($(this).attr('id')=='slctDiv2'){ //do this      }else{ //do that       }  });  


Solution:2

$("#slctDiv2").val();


Solution:3

Wow! Because I failed to post my entire code you guys didn't know that after the within the change event I was doing a $.getJSON so my $(this) I guess was referencing the xhr. Duh. Thanks for (and sorry for wasting) your time!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »