11

What is the best way to call two functions upon a single click event?

There will be multiple elements requiring this script, so the script must act only upon the element being clicked — not all the elements at once.

Let me know if I need to provide more details. Thanks again for all your help.

2 Answers 2

17
$(function() {
    $('a').click(function() {
        func1();
        func2();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

6

jQuery will handle that for you quite nicely, see example:

$('div').click(function(){
    alert($(this).text()+' clicked');
})
.click(function(){
    $(this).css({'color':'#ff0000'});
});
<div>Button 1</div>
<div>Button 2</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Here's a jsFiddle example: http://www.jsfiddle.net/pcASq/

6 Comments

Chaining 2 click events together is wrong. aSeptik has it right.
I fail to see how that is wrong. I demonstrated "how to call two functions" in jQuery. Sure you could combine them (the way that aSeptik did) and use named functions however this is a perfectly valid alternate demonstration of multiple bindings on a single element.
I meant its the wrong way to go about it. It does work but not effeciant
Your answer is perfectly valid and nicely demonstrates multiple bindings on the same event.
It's not "the wrong way to go about it". jQuery supports this functionality because they designed it to work this way. It's perfect OK to do what @JasonBenson suggested.
|

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.