Shortest way of attaching multiple DOM elements to one event handler using Jquery?

Posted by Camsoft on Stack Overflow See other posts from Stack Overflow or by Camsoft
Published on 2010-04-20T12:37:15Z Indexed on 2010/04/20 12:43 UTC
Read the original article Hit count: 338

I need to attach a single event handler to multiple DOM elements using jQuery. I'm currently achieving this using jQuery's each() method. See below:

$([elm1, elm2, elm3]).each(function() {
  this.click(eventHandler);
});

I was wondering if there is a way to do this without using the each method and closure. I thought I would be able to do something like:

$(elm1, elm2, elm3).click(eventHandler);

This kind of statement works using Prototype.js but not in jQuery.The reason I ask the question is because after using both Prototype.js and jQuery I have found that jQuery requires simpler statements to achieve the same tasks in almost every area so I assume there must be a better way of doing this?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about event-handling