7
var $obj1 = $('a#one'); var $obj2 = $('a#two'); var $obj3 = $('a#three'); // Assume $('a#one, a#two, a#three') wasn't an option. 

How do I assign one (same) event handler e.g. click() to those three jQuery objects? Essentially, I am looking for a more efficient way of doing:

$obj1.click(function() { /* Code A */ }); $obj2.click(function() { /* Code A */ }); $obj3.click(function() { /* Code A */ }); 
1
  • 1
    Why not add a class to all the objects and use that as the selector? $('a.codeA').click(function() { /* Code A */ }); Commented Mar 16, 2012 at 7:46

4 Answers 4

9

Only (very) marginally shorter, but:

$([$obj1, $obj2, $obj3]).click(function() { }); 

You would want to define your handler outside of the first anonymous function though, so you'd really probably be better off with Jonas H's method. Just throwing another suggestion out there.

Note: you could technically do a hybrid, but it's quite lengthy:

var f = function() { }; $([$obj1, $obj2, $obj3]).click(f); 

Really this question is only useful if you want to avoid the $a.click(f) over and over again, which is truly a better option than this jQuery abuse. :)

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need the .each loop, because the click handler is attached to all the elements in the current collection. See the first example here api.jquery.com/click
Oh you're right! I always forget that jQuery selectors return normal arrays. Will edit.
Just a quitck note, the array syntax above does not work if you are chaining a function (at least, in jQuery 1.7.2) like jQuery's data function. So this won't work: $([$obj1, $obj2]).data("property",true); but this will: $($obj1, $obj2).data("property",true);. Hope that helps someone.
2

Hmm, there's always .add(), but personally I prefer writing out the .click() functions one by one, if I started out with seperate variables anyway. No need to acquire additional overhead creating new jQuery objects for such trivial functionality.

$obj1.add($obj2).add($obj3).on('click' , function () { } ); // not sure if $obj1.add($obj2, $obj3) will work as well 

1 Comment

$obj1.add($obj2, $obj3) doesn't work i.e. the handler won't be attached to $obj3. Sample: jsfiddle.net/sikusikucom/VsVcy
1
var myFunction = function() { /* Code A */ }; $obj1.click(myFunction); $obj2.click(myFunction); $obj3.click(myFunction); 

Comments

0

get a common parent container and use jQuery .on() on it.

<div id="parent"> <a href="#" id="one">1</a> <a href="#" id="two">2</a> <a href="#" id="three">3</a> </div> //any <a> clicked under parent executes this function $('#parent').on('click','a#one, a#two, a#three',function(){ //do stuff }); 

the best part of it is: it's just one handler and it's in the parent. you aren't putting multiple handlers on multiple elements. plus, when you modify the handler, it takes effect for all. no need to individually change handlers for each element who has it. demo here


http://api.jquery.com/on/

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements:

A delegated-events approach attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody)

5 Comments

And if he has links in the container he doesn't want to operate on?
he can add them in the second parameter, but is still a single handler.
And then he's essentially doing what he said he doesn't want to do :)
the difference between what im doing and what he's doing is that he's adding handlers to each of them, while im giving ONE handler to the parent
"Assume $('a#one, a#two, a#three') wasn't an option" If that's not an option, then $("#parent").on('click', 'a#one...') probably isn't either.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.