Is there an OR clause in jquery selector strings?

I would like to test the element object to see if it has "classA" or "classB". For example, the click () event returned an element in the property of the target event. I want to check this element to see if it has one of these two classes.

 $("#mybutton").click(function($event) { var $el = $($event.target); if($el.is(".classA") || $el.is(".classB")) { // Can I do the above with 1 selector instead of two separate ones? } }); 

As above, using the is() function, I can check each class separately, but I'm curious if I can do this with one function call instead of two?

+7
source share
3 answers

Yes, you can separate the selector with a comma:

 if($el.is(".classA, .classB")) 

Look at the action .

+12
source

You can check if

 if($el.is('.classA, .classB')) { ... } 

Here is a small test case: http://jsfiddle.net/ydtv2/

+1
source

no in jquery there is no condition or condition. You must use the selectors in the same expression as $el.is(".classA, .classB") , and it can handle this as a condition

0
source

All Articles