Nam Nam - 1 year ago 169
Javascript Question

ember-cli How to set default value from form select?

My template:

<select name="lang" id="lang" onchange={{action "selectOption" value="target.value"}}>
<option value="">--Choose--</option>
{{#each model as |lang|}}
<option value="{{lang.url}}">{{lang.title}}</option>

has one of the values:
['php', 'javascript', ...]

My Controller:

lang: 'php', //this is not working, I am thinking of something like lang.url or lang.get('url') but looks like it is still not right ..
actions: {
selectOption: function(option) {
this.set("lang", option);

How can I set the default value of select to php?

Answer Source

Set true to the selected option's selected attribute. Something this:

<option value="{{lang.url}}" selected={{eq lang.url 'php'}}>{{lang.title}}</option>

eq helper can be found here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download