I have a list like this:
<select name="select_list_name" id="list_id">
<option value="">Select Option</option>
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
...
...
</select>
I am trying to get the text value of the currently selected option in a select list. I looked at this thread: jQuery get specific option tag text and tried this:
$("#list_id option:selected").text()
But this only gets me the first options text ("Select Option"
) regardless of which option has been selected.
I tried another way:
$("[name=select_list_name] option:selected").text()
That gets me the first option's text concatenated with the selected options's text ("Select OptionOption 2"
if I select Option 2).
Any idea on why?
Simply try the following code. var text= $('#yourslectbox'). find(":selected"). text();
Get the selected value and text of the dropdown list. If we want to get the selected option text, then we should use the selectedIndex property of the selectbox . The selectedIndex property denotes the index of the selected option. Once we get the selected index, we can access the selected option.
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.
To get the text of the selected option, we first need to find the selected option using the find() method and then use the text() method to get the option text. where selectBox is the value of the id attribute of the <select> HTML tag.
$('#list_id :selected').text();
should give you the selected option's text.
Something else in your code must be wrong -- this piece of code really works
This WORKS, 100%, do you have more than one id with 'list_id'?
$('#list_id :selected').text();
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