Why firstNode always return undefined?


#1

The first node of the template is a button tag and the click function works but always return undefined as the node type/name… “anything” why ?

Template.button.events({
‘click’: function () {
console.log(this.firstNode.elementType); // return undefined
}
});


#2

That’s because firstNode is a property of the Template instance, so what you want is something like:

Template.button.events({
  'click': function (event, template) {
    console.log(template.firstNode.elementType);
  }
});

You can also simplify this code by writing it in ES2015 style:

Template.button.events({
  click(event, template) {
    console.log(template.firstNode.elementType);
  }
});

#3

can you explain more ? what’s the e & t arguments ?


#4

My bad - I’ve updated the examples to be a little less lazy!

You can check the docs for more information:

The handler function receives two arguments: event, an object with information about the event, and template, a template instance for the template where the handler is defined…