Answer: Use the jQuery :selected Selector You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.
$var = jQuery("#dropdownid option:selected"). val(); alert ($var); Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").
Try this:
$('select option:selected').text();
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()
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>
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();
I found this helpful
$('select[name=users] option:selected').text()
When accessing the selector using the this
keyword.
$(this).find('option:selected').text()
Try this:
$('select option:selected').prop('label');
This will pull out the displayed text for both styles of <option>
elements:
<option label="foo"><option>
-> "foo"
<option>bar<option>
-> "bar"
If it has both a label
attribute and text inside the element, it'll use the label
attribute, which is the same behavior as the browser.
For posterity, this was tested under jQuery 3.1.1
$("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.
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