I have the following HTML:
<select id="dropdown">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
I have the string "B" so I want to set the selected
attribute on it so it will be:
<select id="dropdown">
<option>A</option>
<option selected="selected">B</option>
<option>C</option>
</select>
How would I do this in jQuery?
This can be a solution
Something along the lines of...
If you are using JQuery, since the 1.6 you have to use the .prop() method :
You can follow the .selectedIndex strategy of danielrmt, but determine the index based on the text within the option tags like this:
This works on the original HTML without using value attributes.
Code:
Example: