user2971263
user2971263

Reputation: 93

Multiple Button Click Events Jquery On

Can I bind Multiple Button Click events to the same function as below ?

$(document).on("click", "#btn1 #btn2 #btn3", callbackFunction);

Upvotes: 1

Views: 2603

Answers (2)

jhdielman
jhdielman

Reputation: 16

Yes you can. However, you need to separate them with a comma.

$(document).on("click", "#btn1, #btn2, #btn3", callBackFunction);

If possible, it might be wise to assign a class to the buttons and use the class as the seletor. Any button with that class will be bound to the event. This will keep you from having to add additional selectors.

Upvotes: 0

John Koerner
John Koerner

Reputation: 38077

Use commas to separate the values:

$(document).on("click", "#btn1, #btn2, #btn3", callbackFunction);

Then you can determine which one called you by accessing the this object, so the following would alert the id of the element clicked:

$(document).on("click", "#btn1, #btn2, #btn3", function () { 
     alert($(this).attr("id"));
});

For example, this fiddle: http://jsfiddle.net/SFLDw/1/

Upvotes: 2

Related Questions