Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access click event of two buttons using jquery

Tags:

jquery

I have 2 html button named Button1 and Button2.

The two button performs same operation using jquery.

now i wrote the same jquery in click event of both buttons.thats shown below

 $('#Button1').click(function () {
 xyz //some jquery
  })



 $('#Button2').click(function () {
 xyz //some jquery
  })

The both jquery are same. Can i catch the click event of both button in single function?

like image 714
Null Pointer Avatar asked Mar 14 '11 11:03

Null Pointer


2 Answers

You can use a multiple selector:

$('#Button1, #Button2').click(function() {
    // You can use `this` to refer to the source element, for instance:
    $(this).css("color", "red");
});
like image 162
Frédéric Hamidi Avatar answered Oct 21 '22 03:10

Frédéric Hamidi


Declare a function and use it as a parameter. For example:

function handleClick() {
    // Do something..
}

$('#Button1').click(handleClick);
$('#Button2').click(handleClick);

Hope this helped.

like image 27
Kevin Avatar answered Oct 21 '22 03:10

Kevin