0

Everywhere on SO there is only selector via value, but I want to select option based on it text.

For example, I have this:

<select id="some-id">
    <option>1</option>
    <option>2</option>
</select>

How get option with text equal to "2"? Something like this:

jQuery("#some-id option[text='2']")

But it is not working for me, how it should be done?

kamil
  • 3,482
  • 1
  • 40
  • 64

2 Answers2

5

You can use the :contains selector:

var option = jQuery("#some-id option:contains('2')");

Here's a working example.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
4

Something like this should probably work:

$("#some-id option").filter(function() { return $(this).text() == '2'; });
SexyBeast
  • 7,913
  • 28
  • 108
  • 196