JQuery OR Selector?

Just trying to figure out how you can test multiple selectors within an existing piece of code, for example,

if (!(jQuery('#selector1 || #selector2').hasClass('some-class'))) { //code here } 

This does not work, but I wonder if there is anything I can do to make it work?

+4
source share
4 answers

Just select both of them, if each of them has some-class , the condition is true:

 if (!jQuery('#selector1, #selector2').hasClass('some-class')) { //code here } 

Or, if I misinterpreted your logic, you can (and should, for the sake of speed and simplicity) break them into two parts:

 if (!(jQuery('#selector1').hasClass('some-class') || jQuery('#selector2').hasClass('some-class'))) { //code here } 
+12
source

Pretty simple, it's a comma;)

 if (!(jQuery('#selector1 .some-class, #selector2 .some-class'))) { //code here } 
+1
source

not what i know. might just do something like this:

 if((!$('#selectorid1').hasClass(class)) || (!$('#selectorid2').hasClass(class))){} 
+1
source
 if ((!('#selector1').hasClass('some-class')) || (!('#selector2').hasClass('some-class'))) { //code here } 

Why not?

0
source

All Articles