I have a select control, and in a javascript variable I have a text string.
Using jQuery I want to set the selected element of the select control to be the item with
var text1 = 'Two';
$("select option").filter(function() {
//may want to use $.trim in here
return $(this).text() == text1;
}).prop('selected', true);
var text1 = 'Two';
$("select option").filter(function() {
//may want to use $.trim in here
return $(this).text() == text1;
}).attr('selected', true);
Note that while this approach will work in versions that are above 1.6 but less than 1.9, it has been deprecated since 1.6. It will not work in jQuery 1.9+.
val()
should handle both cases.
$('select').val('1'); // selects "Two"
$('select').val('Two'); // also selects "Two"