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.

1 comment:

  1. Coin Casino Review 2021 | Claim up to $1000 in Bonus
    Coin Casino 인카지노 is an online casino that launched a mobile app in the spring of 제왕카지노 2018. They're available in several languages, including English.What is Coin Casino?What is the minimum deposit at Coin Casino? 온카지노

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...