Reputation: 171449
To get the text of the selected option I do:
$("select option:selected").text()
How could I set an option based on it's text ?
Upvotes: 1
Views: 342
Reputation: 2995
This would work:
$('select option:contains("C")').attr('selected', true);
Example: http://jsfiddle.net/ADp6L/1/
Upvotes: 3
Reputation: 2039
Clear the old selection out first and then loop through the options, canceling the loop once the correction option is found.
$("select option").removeAttr("selected").each(function() {
var jThis = $(this);
if (jThis.text() == "mytext") {
jThis.attr("selected", "selected");
return false;
}
});
Upvotes: 4