jQuery template select option

2.9k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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>
5
On
$("select").val(data['color']);