Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery OR Selector?

Just trying to figure out how you can test multiple selectors within an existing piece of code i.e. like this

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

This doesn't work but wondering if there is anything I can do to make it work ?

like image 879
Tom Avatar asked Dec 10 '22 13:12

Tom


2 Answers

Just select them both, if either of them has the class 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 sake of speed and simplicity) break those into two:

if (!(jQuery('#selector1').hasClass('some-class') || jQuery('#selector2').hasClass('some-class'))) {
    //code here
}
like image 199
Tatu Ulmanen Avatar answered Dec 28 '22 22:12

Tatu Ulmanen


Quite simple, it's a comma ;)

if (!(jQuery('#selector1 .some-class, #selector2 .some-class'))) {
//code here
}
like image 32
Wolph Avatar answered Dec 28 '22 22:12

Wolph