Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor JS: How to do I create event handler for multiple selectors?

I am trying to create the same event handler for multiple elements, but cannot find anywhere in the documentation to do this. In the below example, I am trying to create a click handler for all text handings. This works for h1, but not for the rest.

Template.page.events({   'click h1, h2, h3, h4, h5, h6' : function (e, template) {     console.log("clicked");   } } 
like image 572
Chanpory Avatar asked Feb 03 '14 06:02

Chanpory


People also ask

How do you add multiple event handlers?

The addEventListener() method You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object. The addEventListener() method makes it easier to control how the event reacts to bubbling.

Can we have more than one event handler method for same event?

One event can have more than one event handler method in the same class or in different classes. At the run time only one handler method will be triggered at a time. After triggering the one that has to be deactivated and then another handler method will have to be registered to be triggered.

How does event handlers work JavaScript?

JavaScript Event HandlersEvent handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads. Things that should be done when the page is closed. Action that should be performed when a user clicks a button.

What is an object event handler in JavaScript?

The event handler can trigger an object method or any general activity when the event occurs. JavaScript applications are largely event-driven. Other event examples are: click - clicking a button is an event; change - changing a text field; and select - selecting text from a text field.


2 Answers

Try this:

Template.page.events({   'click h1, click h2, click h3, click h4, click h5, click h6' : function (e, template) {     console.log("clicked");   } } 

I believe event maps do not support comma separated selectors because commas are used to delimit individual event names or event selector pairs.

like image 155
sbking Avatar answered Nov 08 '22 18:11

sbking


http://docs.meteor.com/#eventmaps

Template.page.events({    'click h1, click h2, click h3, click h4, click h5, click h6' : function (e, template) {     console.log("clicked");   } } 
like image 25
Peppe L-G Avatar answered Nov 08 '22 18:11

Peppe L-G