8

How do I register multiple callbacks for a jQuery event? An example of what I am trying to achieve:

$(document).on("click", ".someclass", CallbackFunction1, CallbackFunction2);

function CallbackFunction1(event) {
    //Do stuff
}

function CallbackFunction2(event) {
    //Do some other stuff
}

How can I set up the event handler to execute both callback functions when the element is clicked?

4 Answers 4

15

You can just attach them as separate event handlers:

$(document).on("click", ".someclass", CallbackFunction1)
           .on("click", ".someclass", CallbackFunction2);
Sign up to request clarification or add additional context in comments.

3 Comments

Did just that. Thank you very much!
I am trying this, but I find that if I register about 40 callbacks that performance seriously deteriorates (functions take as long as 10 seconds to start executing)
I just tried adding a lot of click listeners at document.body on this page via the console, and didn't see any noticeable slowdown. There is something specific to your setup which causes it.
5

Unless I misunderstand what you're asking, you can use a single event handler:

$(document).on('click', '.someclass', function(e){
    CallbackFunction1(e);
    CallbackFunction2(e);
});

2 Comments

That implementation lacks a few things. It doesn't set the this value of the functions, it doesn't handle return values, and it doesn't check to see if immediate propagation was stopped between calls.
@cookiemonster : A way to get this and allow for return values (that you wouldn't really work with on click event handler as such anyway) would be: CallbackFunction1.apply(this,[e]); CallbackFunction2.apply(this,[e]). I use this methodology often even if only need a single callback. It allows me to decouple my callbacks from relying too heavily on the type of event that generates them.
0

You can use a third function and then recall the other ones:

$(document).on("click", ".someclass", CallbackFunction);

function CallbackFunction(event) {
    CallbackFunction1(event);
    CallbackFunction2(event);
}

function CallbackFunction1(event) {
    //Do stuff
}

function CallbackFunction2(event) {
    //Do some other stuff
}

5 Comments

we could go even deeper and call it 'func-ception'! ;-)
Yup, i know. I didn't downvote either though. I just upvoted Vatev's answer since that is the most elegant (and correct) solution in this case :-)
Sure, I totally agree with you and was just trying to justify my answer! Anyway I really appreciated the term "func-ception", I'll remember that forever!!! :D Respect++ for you! :D
Don't know why you got downvoted because this works too
@exantas the reason this got downvoted is because it's a bad practice to do this ánd, as can be read in the comments on AJM's response :-) Putting a nail into a wall with your head doesn't make it a hammer... it makes it very sore. But it does work!
0

If you will be reusing this to bind different list of handlers for different elements, i would create a factory.

function multiFunction(){
    var methods = Array.prototype.slice.call(arguments, 0);
    return function(e){
        for (var f=0, l = methods.length; f<l; f++) {
            methods[f].apply(this, arguments);
        }
    }
}

and call this like this

$(document)
    .on('click', 'someclass', multiFunction( CallbackFunction1, CallbackFunction2));
    .on('click', 'someotherclass', multiFunction( CallbackFunction8, CallbackFunction1, CallbackFunction5));

Demo at http://jsfiddle.net/gaby/D8K75/

1 Comment

@cookiemonster right.. was testing something else before that solution and it stuck.. also changed call to apply so that it can pass all arguments supported by event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.