Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

JQuery get specific option tag text

I have this:
<select id='list'>
<option value='1'>Option A</option>
<option value='2'>Option B</option>
<option value='3'>Option C</option>
</select>


What might the selector resemble in the event that I needed to get "Alternative B" when I have the value '2'?

Kindly note that this isn't requesting that how get the chosen text value, however, any of them, if chosen, depending upon the value trait. I attempted:

$("#list[value='2']").text();

But it is not working.
by

2 Answers

aashaykumar
It's looking for an element with id list which has a property value equal to 2.
What you want is the option child of the list:

$("#list option[value='2']").text()
kshitijrana14
If you'd like to get the option with a value of 2, use
$("#list option[value='2']").text();

If you'd like to get whichever option is currently selected, use
$("#list option:selected").text();

Login / Signup to Answer the Question.