<select id="sel">
<option id="1">aa</option>
<option id="2">bb</option>
<option id="3">cc</option>
</select>
$("#sel").change(function(){
alert($(this).children().attr('id'))
})
LIVE: http://jsfiddle.net/cxWVP/
How can i get current selected option ID? Now always show me id="1".
var yourSelect = document. getElementById("your-select-id"); alert(yourSelect. selectedOptions[0]. value);
The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
$('#sel').change(function(){
alert($(this).find('option:selected').attr('id'));
});
should work.
http://jsfiddle.net/cxWVP/1/
$("#sel").change(function(){
alert( this.options[this.selectedIndex].id );
})
<option>
tags should have a value
attribute. When one is selected, you get it's value using $("#sel").val()
.
<select id="sel">
<option value="1">aa</option>
<option value="2">bb</option>
<option value="3">cc</option>
</select>
$("#sel").change(function(){
alert($(this).val())
});
DEMO: http://jsfiddle.net/yPYL5/
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 )
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With