Why firstNode always return undefined?

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
}
});

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);
  }
});
1 Like

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

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…

1 Like