2

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

2
  • Please share the code you have tried. Commented Sep 29, 2014 at 12:37
  • you need to put this country variable in cookie or something to set it's value on the other page . If you are handling this by C# , then do tell me . Commented Sep 29, 2014 at 13:09

3 Answers 3

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

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());

Comments

0

$('#select-list option["' + country + '"]').prop('selected', true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.