Skip to content Skip to sidebar Skip to footer

Get Current Selected Option

$('#sel').

Solution 1:

$('#sel').change(function(){
   alert($(this).find('option:selected').attr('id'));
});

should work.

Solution 2:

http://jsfiddle.net/cxWVP/1/

$("#sel").change(function(){
   alert( this.options[this.selectedIndex].id );
})

Solution 3:

<option> tags should have a value attribute. When one is selected, you get it's value using $("#sel").val().

<select id="sel">
    <optionvalue="1">aa</option><optionvalue="2">bb</option><optionvalue="3">cc</option>
</select>

$("#sel").change(function(){
   alert($(this).val())
});

DEMO: http://jsfiddle.net/yPYL5/

Solution 4:

http://jsfiddle.net/ugUYA/

To get the selected option inside a select element, you can use selectedIndex, then you can use the options array to get to the selected option object

$("#sel").change(function(){
   alert( this.options[this.selectedIndex].id )
})

Solution 5:

Try this

$("#sel").find('option:selected').attr('id');

Ideally you should use value attribute for option tags and just use val method to get the selected value from the dropdown element. $("#sel").val();

Post a Comment for "Get Current Selected Option"