Reputation: 103
User has selected a country from select list. The option is stored with var country = $('#select-list option:selected').text();
Now when user opens account edit page I would need the country to be selected by default when the page opens.
Only data I could use is the var country
which could be in this case ´Great Britain´ and I need jquery to present Great Britain from the select list as selected. How should I do this
Upvotes: 2
Views: 3270
Reputation: 11
$('#select-list option["' + country + '"]').prop('selected', true)
Upvotes: 0
Reputation: 28513
Try this : use .filter
to get the option which has same text as country variable value and set its value to select box
$('#select-list').val( $( '#select-list option').filter(function(){ return $(this).text()==country;}).val());
Upvotes: 0
Reputation: 27765
Using jQuery you can try this using contains
selector. Like this:
$('#select-list').val( $( '#select-list option:contains("' + country + '")' ).val() );
And if value
attribute in your <option>
elements equal text content you can do this using only val()
method:
$('#select-list').val( country );
Upvotes: 1