0

I have select tag with a lot of options inside. I have to set selected some option with text value = %value% after page is loaded (jquery). How can I do this?

aynber
  • 22,380
  • 8
  • 50
  • 63
Max Frai
  • 61,946
  • 78
  • 197
  • 306
  • possible duplicate of [jquery : check if select contains option?](http://stackoverflow.com/questions/3744289/jquery-check-if-select-contains-option) or [Use jquery select an option](http://stackoverflow.com/questions/2323607/use-jquery-select-an-option), depending on whether you want to select by value or by text. – Felix Kling Feb 19 '11 at 22:21

2 Answers2

0

This works:

$("option[value='%value%']").attr("selected",true);
KeatsKelleher
  • 10,015
  • 4
  • 45
  • 52
  • Umm, you have true wrapped in quotes. You should either lose the quotes, or do what I did in my answer `.attr('selected, 'selected')` – Eli Feb 19 '11 at 22:43
  • `selected` is a boolean attribute, to validate it either needs to be present, or have it's value set to itself (`selected="selected"`). That all being said: `selected="true"` will *work*, but is not **valid**. – zzzzBov Feb 19 '11 at 23:09
0
$('option[value=' + somevalue + ']').attr('selected', 'selected'); 
Eli
  • 17,397
  • 4
  • 36
  • 49