Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Functions on a Single Event with JQuery/Javascript

Tags:

jquery

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.

like image 740
Rrryyyaaannn Avatar asked Feb 09 '11 22:02

Rrryyyaaannn


2 Answers

$(function() {
    $('a').click(function() {
        func1();
        func2();
    });
});
like image 90
Luca Filosofi Avatar answered Oct 23 '22 11:10

Luca Filosofi


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/

like image 20
Jason Benson Avatar answered Oct 23 '22 11:10

Jason Benson