1

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

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

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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.