To use in jQuery hasClass Method to get a specific element without a given class, we can use the hasClass
method with the !
operator to find the element without the given class.
For instance, if we have the following HTML:
<div id='foo'>
foo
</div>
Then we can use:
if (!$('#foo').hasClass('bar')) {
//...
}
to check the div with ID foo
has the class bar
.
hasClass
returns true
if the element has the even class, so we can negate that with the !
operator.
If it doesn’t then we do something in the if
statement body.