Mark
Mark

Reputation: 33621

jQuery template select option

How do I use jQuery templates to select an option?

Let's say my data is: { "color" : "red" }

I have :

<select>
  <option>blue</option>
  <option>green</option>
  <option>red</option>
</select>

I want red to be the one that's selected by default. How can I do this?

Thanks.

Upvotes: 0

Views: 2966

Answers (2)

RP Niemeyer
RP Niemeyer

Reputation: 114792

Maybe not the most elegant, but here is a way to do it in your template: http://jsfiddle.net/rniemeyer/z7Uu7/

<select>
    <option {{if color == 'blue'}}selected{{/if}}>blue</option>
    <option {{if color == 'green'}}selected{{/if}}>green</option>
    <option {{if color == 'red'}}selected{{/if}}>red</option>
</select>

Upvotes: 3

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17762

$("select").val(data['color']);

Upvotes: 6

Related Questions