Search code examples
jqueryember.jsember-cli

Set value to input dynamically in ember


I am trying to set variable name to input value so the data test selector value is same when value is entered in input box. if i currently use value=item.name

it displays the name in input box. i juzt want value to be set as variable name when value in input entered it is also assigned to data-test-selector. like old code as shown below.

Trying to use dynamically using each

{{#each items as |item|}}
        <input id={{item.name}} maxlength="15" class="form-control" data-test-License={{item.name}} {{action "myAction" onEvent="keyUp"}} value=item.name/>
{{/each}}

old code

{{input type="text" maxlength="15" class="form-control" value=product data-test-selector=product}}

Solution

    1. You forgot to wrap item.name with {{.
    2. there is no onEvent. but there is on like on="keyUp" refer guide section Specifying the Type of Event
    3. myAction method should update the value to item.name property or you could try the below.

      <input id={{item.name}} maxlength="15" class="form-control" data-test-License={{item.name}} value={{item.name}} oninput={{action (mut item.name) value="target.value"}}>


    Update:
    can we add observer or action event where we allow user to only enter numeric and no alphabets?
    You dont need to use observer in this case, oninput={{action 'updateName' item}}

    updateName(item, event){
      let number = event.target.value.toString().replace(/[^\d.]/g, "");
      Ember.set(item,'name',number);
    }