How to select an option by its option text using jQuery

You, JavaScriptjQuery

Okay so here is a select list below and you need to choose an option by text so you can mark it as selected or trigger a change etc. 

Select from option value is pretty easy and it is as below:

$('select option[value="test"]').val();

So now selecting by text, If you are using jQuery and if you have an older version than 1.9 the following will work choosing by text.

$('select option[value="label"]').attr('selected', true);

For more recent versions of jQeury the following works.

$('select option:contains("label")').attr('selected', true);
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.