Dave
Dave

Reputation: 9147

Help me with the "or" jquery selector?

I have this code:

$("#leftbutton").click(function(e){
 /// do xyz
});

$("#rightbutton").click(function(e){
 /// do xyz (same thing)
});

How do I combine them so that if you click #leftbutton or #rightbutton it does xyz?

Something like this:

$("#leftbutton OR #rightbutton").click(function(e){
 /// do xyz
});

Upvotes: 3

Views: 152

Answers (3)

BoltClock
BoltClock

Reputation: 723538

Use the comma, also known in the jQuery docs as the multiple selector:

$("#leftbutton, #rightbutton").click(function(e){
 /// do xyz
});

Upvotes: 7

robbrit
robbrit

Reputation: 17960

Just do:

$("#leftbutton, #rightbutton").click(function(e){
  ...
});

Upvotes: 1

Harmen
Harmen

Reputation: 22438

Just like CSS selectors, you could use the comma

$("#leftbutton, #rightbutton").click(function(e){
 /// do xyz
});

Upvotes: 1

Related Questions