Which selector do I need to select an option by its text?
This could help:
$('#test').find('option[text="B"]').val();
Demo fiddle
This would give you the option with text B
and not the ones which has text that contains B
.
For recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();
Updated fiddle
You can use the :contains()
selector to select elements that contain specific text.
For example:
$('#mySelect option:contains(abc)')
To check whether a given <select>
element has such an option, use the .has()
method:
if (mySelect.has('option:contains(abc)').length)
To find all <select>
s that contain such an option, use the :has()
selector:
$('select:has(option:contains(abc))')