views:

43

answers:

4

Is this possible?

Code:

$('#goRightBtn').click(function() {
        Random code here
};

I have a button, when you click that button (goRightBtn) it will do all of the functions and whatnot inside that code up there. But I also have another button that will do the exact same thing (don't ask why.. Just need it!) How do I make it so that I can somehow make that code work when either one of the btns are selected..

Would something like

$('#goRightBtn','#secondBtn').click(function() {
        ///Random code here
};

work?

Please help! :)

+3  A: 
$('#goRightBtn, #secondBtn').click(function() {}

If you specify a second argument, that means you are specifying the context. Otherwise just put the ids in your initial argument's string like above.

http://api.jquery.com/multiple-selector/

meder
Ahh Thank you so much! works perfectly.
Annie
+2  A: 

The following should work (a comma wrapping them)

$('#goRightBtn, #secondBtn').click(function() {
        ///Random code here
};

Or you can use the add method also:

$('#goRightBtn').add('#secondBtn').click(function() {
        ///Random code here
};
Sarfraz
The first one is incorrect, that would give the selector a context...
Nick Craver
@Nick Craver: Yeah spotted that before your comment :)
Sarfraz
+1  A: 

Almost had it right :) It's like this (called a multiple selector):

$('#goRightBtn, #secondBtn').click(function() {
  ///Random code here
});
Nick Craver
+1  A: 

working demo

$('#goRightBtn, #secondBtn').click(function() { 
        alert('test');
});
Andreas Niedermair
You should post code in the answer as well not just a demo (though something this simple doesn't need one, IMO). If your demo site goes offline for any reason, the answer becomes useless :)
Nick Craver
thanks pal :) even though i'm not in complete agreement :)
Andreas Niedermair