javascriptjqueryjquery-selectorshtml-select

jQuery get specific option tag text


Suppose I have a drop-down list like:

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

Given the value '2' (i.e., using a specific value, not necessarily the selected one), what selector can I use to get "Option B"?

I tried $("#list[value='2']").text();, but it is not working.


Solution

  • 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()