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);

Answer

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/

Leave a Reply

Your email address will not be published. Required fields are marked *