Daniel.P.
Daniel.P.

Reputation: 480

Handling select tag via jQuery

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.

Upvotes: 0

Views: 512

Answers (1)

mVChr
mVChr

Reputation: 50215

$('#lang').find('option').attr('selected', false);
$('#lang').find('option[value="'+ajaxReturn+'"]').attr('selected', true);

Upvotes: 1

Related Questions