Jquery set selected option text with its value

Given this HTML:

< select >
    
    
Select by description for jQuery v1.6+:
var text1 = 'Two';
$("select option").filter(function() {
    //may want to use $.trim in here
    return $(this).text() == text1; 
}).prop('selected', true);
Select by description for jQuery versions below 1.6 and greater than or equal to 1.4:
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.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...