0

I cant figure how to simply solve this problem:

So I have simple HTML

<select id="lang" name="lang">
<option value="cs" selected="selected">Czech</option>
<option value="en">English</option>
<option value="it">Italian</option>
</select>

On the same page, I'am using ajax call to received information, If user hasn't already select language (that is not that much important). But for example: I get from ajax call value: "en". So I need to remove attribute "selected" and add it to the right option tag. (option tag where is attribute value="en"). I believe there is a simple solution, but I don't have enough skills with jQuery.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Daniel.P.
  • 480
  • 2
  • 8
  • 19
  • possible duplicate of [Set selected option of select box](http://stackoverflow.com/questions/4680075/set-selected-option-of-select-box) – Felix Kling May 02 '11 at 22:14
  • 1
    Since the question didn't get closed and you accepted the only answer, a shorter way is: `$('#lang').val(newValue)` – Felix Kling May 02 '11 at 23:16

1 Answers1

1
$('#lang').find('option').attr('selected', false);
$('#lang').find('option[value="'+ajaxReturn+'"]').attr('selected', true);
mVChr
  • 49,587
  • 11
  • 107
  • 104