event.target is undefined in events

Ramesh Murugesan picture Ramesh Murugesan · May 20, 2015 · Viewed 42.8k times · Source

How can one use each input values in events? Hope my below code will explain you well.

HTML:

<template name="UpdateAge">
    {{#each Name}}
    <div data-action="showPrompt">
       <div>
          Some content
       </div>

       <div>
         <div>
            Some content
         </div>
       </div>
       <div>
           Name : {{name}}
           Age : <input type="text" name="age" value="{{age}}"/> //I need to access age values in event
       </div>
    </div>
{{/each}}
<template>

JS:

Template.UpdateAge.events({
  'click [data-action="showPrompt"]': function (event, template) {
        console.log(event.target.age.value); // TypeError: event.target.age.value is undefined
  }
});

I dont know whether my approach is good for passing parameter values to events so suggestions are welcome.

Answer

user757095 picture user757095 · May 20, 2015

the notation you are using is valid only for form elements and when event.target IS a form element.

event.target.age.value

event.target is the element where the event occurred, event.currentTarget is the element which has attached the event handler

if you replace div with form and replace target with currentTarget then it will work

event.currentTarget.age.value

here a fiddle using your code