Help me with a "or" jquery selector?

I have this code:

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

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

How to combine them so that when you click #leftbutton or #rightbutton xyz?

Something like that:

$("#leftbutton OR #rightbutton").click(function(e){
 /// do xyz
});
+3
source share
3 answers

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

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

Like CSS selectors, you can use a comma

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

Just do:

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

Source: https://habr.com/ru/post/1775710/


All Articles