1

Here I am having two buttons with id frm_submit1 and frm_submit2

<button type="button" id="frm_submit1">Click Me 1!</button>
<button type="button" id="frm_submit2">Click Me 2!</button>

In jquery, I need to do same onclick event for both the buttons. For one button (frm_submit1) click, I used like below. How can I use for multiple buttons?

$(document).on('click', '#frm_submit1', function(event) {
});

I used this, but didn't worked! Any idea?

$("#frm_submit1, #frm_submit2").click(function (e) {  
// do something  
});
3
  • With the selector $("#frm_submit1, #frm_submit2"), you achieved the objective of attaching the same onclick handler to the two buttons. So what is it that still doesn't work? Commented Nov 18, 2014 at 15:19
  • This will work. But, I don't know the reason. I think problem with my code. Let me check. I am using onsenui Framework. So all the pages will be stored in a stack. So that might be a problem Commented Nov 18, 2014 at 15:22
  • onsenui is pretty arcane. You'll need to provide a link and include the onsen-ui tag. Commented Nov 18, 2014 at 15:26

3 Answers 3

6

Just separate the IDs by a comma:

$(document).on('click', '#frm_submit1, #frm_submit2', function(event) {
});

jsFiddle example

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

8 Comments

@NicholasKyriakides - Perhaps they're added dynamically to the page.
@NicholasKyriakides Yes, why?
I'd prefer using a class as one single selector
This is a bad answer because it does not explain why this would work... it has nothing to do with IDs being separated by commas. And this is placing the listener on the document something that should be placed on the closest static parent -1
@Phil - First off, not a bad answer. I simply re-used the example the OP posted which he said worked for a single element, and extended it. While I agree with you that document is a worst case selector, the OP has provided no other details.
|
1

It's better for this case to use class :

$(".classForButtons").click(function () {  
        // do something  
   });

Comments

-3

Try to use the button element as the selector instead. That will run on click on all buttons. Best would be do declare a class on the buttons that you want to use it on and then use the class as the selector instead.

$("button").click(function (e) {  
    // do something  
});

6 Comments

are you talking seriously?
what is this? Mr.user2952238
Yes, why? This buttons could be added to the DOM dynamically or whatever. That info is not provided. ALSO: "Best would be do declare a class on the buttons that you want to use it on and then use the class as the selector instead."
So, if they are dynamically added, does attaching a click handler to do just one specific thing, to all buttons on the page solve the problem? You need some revision
so in html page are many button added, your code has been add event to all button in page, it's unnecessary..
|

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.