user1017882
user1017882

Reputation:

Select a <select> <option> based on it's value containing a string

<select id='mySelect'>
<option value='FirstOption'>
Option 1
</option>
<option value='SecondOption'>
Option 2
</option>
</select>

I'd like to select the 2nd option based on the fact it has the word 'Second' in it's name.

What I've Tried

$('#mySelect').val('SecondOption');

Closest I've got. Obviously returns second option, but wouldn't work for 'AnotherSecondOption'

Upvotes: 0

Views: 1237

Answers (1)

Try * attribute-contains-selector

$('#mySelect option[value*="Second"]').prop('selected',true);

Upvotes: 2

Related Questions