views:

50

answers:

2

I'd like to execute some javascript if button 1 is pressed OR button 2 is pressed.

I'm using the notation pasted below, (The code below does not work but I've supplied it to illustrate what I am trying to do). I know I can wrap up the code to execute into another function, and then write two blocks of code (1 for each button) but I'd just like to learn if there's a way to do a conditional OR selector in jQuery using the type of sytnax below. Thanks!

$(".btn1.btn2").click(function(){
  //execute code
}
+1  A: 
$("#btn1, #btn2").click(function(){
  //execute code
}

if you have id's for the button.

$(".btn1, .btn2").click(function(){
  //execute code
}

if you want to access them using class name.

rahul
+1  A: 

Use a comma for multiple selections in one call. The example below selects all elements that have class btn1 and/or btn2

$(".btn1, .btn2").click(function(){
  //execute code
}
micahwittman
thanks! i always find learning the syntax takes the longest :P
justinl
You're welcome. Stick with it! :)
micahwittman