tags:

views:

151

answers:

3

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.

+2  A: 

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 
    }
});
Darryl Hein
yes, it is the select element that raise the event not the option element
Marwan Aouida
$('#slctDiv1,#slctDiv2').change(function(){ if($(this).parent().attr('id')=='slctDiv2'){alert('got here');}else{ //do that }});When I change the code to this the alert never gets called. I must be doing something stupid.
A: 

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

Rich Kroll
A: 

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!