Categories
JavaScript Answers jQuery

How to Use in jQuery hasClass Method to Get a Specific Element Without a Given Class?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.