Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery getting text value for a select list

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?

like image 961
Eqbal Avatar asked Feb 28 '10 19:02

Eqbal


People also ask

How do I get text from a dropdown list?

Simply try the following code. var text= $('#yourslectbox'). find(":selected"). text();

How do I get a dropdown selected value and 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.

How do you get the text value of a selected option consider the Select ID select?

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.

How do I get selected text option in select?

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.


2 Answers

$('#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

like image 146
Harmen Avatar answered Oct 09 '22 16:10

Harmen


This WORKS, 100%, do you have more than one id with 'list_id'?

$('#list_id :selected').text();
like image 45
Rippo Avatar answered Oct 09 '22 15:10

Rippo