I need to check if a <select>
has an option whose text is equal to a specific value.
For example, if there's an <option value="123">abc</option>
, I would be looking for "abc".
Is there a selector to do this?
Im looking for something similar to $('#select option[value="123"]');
but for the text.
I tried a few of these things until I got one to work in both Firefox and IE. This is what I came up with.
another way of writing it in a more readable fasion:
Pseudocode:
This works for me
Thanks for all the posts.
I faced the same issue below is the working code :
Demo : http://jsfiddle.net/YRBrp/83/
This will work in jQuery 1.6 (note colon before the opening bracket), but fails on the newer releases (1.10 at the time).
This worked for me:
$("#test").find("option:contains('abc')");
You can use the
:contains()
selector to select elements that contain specific text.For example:
To check whether a given
<select>
element has such an option, use the.has()
method:To find all
<select>
s that contain such an option, use the:has()
selector: