Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a selector have a certain set of classes

I'm trying to be able to check if a selector have a certain sets of classes.

.hasClass() can only check if the selector has one class. And the .is() selector can look for multiple classes but will return true if the selector have at least one of the classes.

But I wan't to achieve a way to check if a selector have both of the classes, and only if it has both of the classes do action.

Any pointers?

like image 950
emilolsson Avatar asked Dec 28 '22 19:12

emilolsson


2 Answers

You can simple list the class names:

.foo.bar.bar

This selector matches any element that has all the classes foo, bar, and baz.

like image 138
Gumbo Avatar answered Dec 31 '22 08:12

Gumbo


The is() function should work for you.

  • If you have an element and you write is(".foo") then it will return true if foo is present.
  • if you write is(".foo.bar") then it will return true if foo AND bar is present.
  • If you write is(".foo,.bar") then it will return true if foo OR bar is present.
like image 42
John Hartsock Avatar answered Dec 31 '22 09:12

John Hartsock