Get current selected option

<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".

Answers:

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

should work.

Answer

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

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

<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/

Answer

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 )
})
Answer

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();

Answer

$(this).find('option:selected').attr('id')

Answer

Try this :

$('select:#idSelect').val()
Answer

Change the id attribute to value and then use .val().

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


var selectedValue = $("#sel").val();
Answer

Will the jquery :selected filter work. Something like this...

$("#sel:selected").each(function(){
   alert($(this).attr('id'))
})

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.