Advertisement
Advertisement


How to get label of select option with jQuery?


Question

<select>
<option value="test">label </option>
</select>

The value can be retrieved by $select.val().

What about the label?

Is there a solution that will work in IE6?

2010/02/01
1
114
2/1/2010 9:43:07 AM

Accepted Answer

Try this:

$('select option:selected').text();
2010/02/01
222
2/1/2010 9:46:15 AM

Hi first give an id to the select as

<select id=theid>
<option value="test">label </option>
</select>

then you can call the selected label like that:

jQuery('#theid option:selected').text()
2012/08/13

For reference there is also a secondary label attribute on the option tag:

//returns "GET THIS" when option is selected
$('#selecter :selected').attr('label'); 

Html

<select id="selecter">
<option value="test" label="GET THIS">
Option (also called label)</option>
</select>
2014/10/09

To get the label of a specific option in a dropdown yo can ty this --

$('.class_of_dropdown > option[value='value_to_be_searched']').html();

or

$('#id_of_dropdown > option[value='value_to_be_Searched']').html();
2013/03/01

I found this helpful

$('select[name=users] option:selected').text()

When accessing the selector using the this keyword.

$(this).find('option:selected').text()
2019/07/29

$("select#selectbox option:eq(0)").text()

The 0 index in the "option:eq(0)" can be exchanged for whichever indexed option you'd like to retrieve.

This is helpful: http://www.myphpetc.com/2009/03/jquery-select-element-cheat-sheet.html

2011/04/18

Source: https://stackoverflow.com/questions/2175737
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]