sajan sajan - 1 year ago 60
jQuery Question

JQuery can't find select options by text

I have a select element with 10 options . I am trying to select a specific option by its text. when I do -

$('#category_id option:last').text();

I get result as "Complete plant" but when I do -

$('#equipment_category_id option[text="Complete plant"]');

it returns a blank array.

Answer Source

Use :contains() which selects all elements that contain the specified text.

$('#equipment_category_id option:contains(Complete plant)')

or use filter() method for exact match

$('#equipment_category_id option').filter(function(){ return $(this).text() == 'Complete plant' })