Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use hasClass to detect if a class is NOT the class I want?

How do I use hasClass so it works as doesNotHaveClass? In other words, rather than looking to see if it is a specified class, looking to see if it is NOT a specified class. Can I use an exclamation point or something like that?

like image 240
sehummel Avatar asked Dec 23 '10 19:12

sehummel


People also ask

How do you check if an element has a specific class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How do you know if an element does not have a class?

Use the classList. contains() method to check if an element does not have a specific class, e.g. if (! el. classList.

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


1 Answers

Yes, you can.

if (!$('element').hasClass('do-not-want')) {
    // This element does not have the .do-not-want class
}
like image 185
BoltClock Avatar answered Oct 09 '22 03:10

BoltClock