Chanpory
Chanpory

Reputation: 3095

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

Upvotes: 27

Views: 10908

Answers (3)

FullStack
FullStack

Reputation: 6020

I solved a similar problem previously, which is reproduced below for handling multiple events on the entire document with a single handler:

Template.template_name_here.events({
  'keyup, click': function(event) {
    event.preventDefault();
    console.log("KEYUP OR CLICK");
  }
});

Upvotes: 1

Peppe L-G
Peppe L-G

Reputation: 8345

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

Upvotes: 2

sbking
sbking

Reputation: 7680

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.

Upvotes: 53

Related Questions