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.
This could help:
Demo fiddle
This would give you the option with text
B
and not the ones which has text that containsB
. Hope this helpsFor recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:
Updated fiddle
As described in this answer, you can easily create your own selector for hasText. This allows you to find the option with
$('#test').find('option:hastText("B")').val();
Here's the hasText method I added:
Use following
For jquery version 1.10.2 below worked for me
This will also work.
$('#test').find("select option:contains('B')").filter(":selected");
This is the best method for select text in dropdownlist.