element.getAttribute is not a function

I’m trying to use a complex selector for the first time in Meteor Blaze Template events and I’m getting this error. Here is my code:
'click #myTab a'(event) { event.preventDefault(); $(this).tab('show'); }
The code performs expected functionality in the UI but i get this in my browser console each time i click the link:
Uncaught TypeError: element.getAttribute is not a function

I was able to solve this by placing the code inside of Template onRendered

Looks like you’re expecting this to be the element.
If you take a look at the docs for eventMaps, this will be your data context:

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. The handler also receives some additional context data in this, depending on the context of the current element handling the event. In a template, an element’s context is the data context where that element occurs, which is set by block helpers such as #with and #each.

What you probably want is event.currentTarget, which will be the element that received the event:

'click #myTab a'(event) { 
     event.preventDefault(); 
     $(event.currentTarget).tab('show'); 
}
1 Like

Oh. Thanks for the explanation. I now understand why

1 Like