Eqbal
Eqbal

Reputation: 4880

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?

Upvotes: 5

Views: 18958

Answers (2)

Rippo
Rippo

Reputation: 22424

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

$('#list_id :selected').text();

Upvotes: 2

Harmen
Harmen

Reputation: 22438

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

Upvotes: 12

Related Questions