How to check an element contains a class using jQuery?
Method 1: Using hasClass() method: The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. It returns a boolean value specifying whether the class exists in the element or not. This can be used to check for multiple classes.
Syntax:
$('element').hasClass('className')
Example:
<!DOCTYPE html> <html> <head> <title> How to check an element contains a class using jQuery? </title> <script src= </script> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to check an element contains a class using jQuery? </b> <p class="example-class"> This element has "example-class". </p> <p>Does the element have class? <span class="output"></span> </p> <button onclick="checkForClass()"> Click to check </button> <!-- Script to use hasClass() method to check the existence of class name --> <script type="text/javascript"> function checkForClass() { classCheck = $('p').is('.example-class'); document.querySelector('.output').textContent = classCheck; } </script> </body> </html> |
Output:

Method 2: Using is() method: This is similar to the above method and can be used to check more attributes if needed. The required element is selected and the class name is passed on like a CSS class selector. It returns a boolean value specifying whether the class exists in the element or not. This can be used to check for multiple classes.
Syntax:
$('element').is('.className')
Example:
<!DOCTYPE html> <html> <head> <title> How to check an element contains a class using jQuery? </title> <script src= </script> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to check an element contains a class using jQuery? </b> <p class="example-class"> This element has "example-class". </p> <p>Does the element have class? <span class="output"></span> </p> <button onclick="checkForClass()"> Click to check </button> <!-- Script to use is() method to check the existence of class name --> <script type="text/javascript"> function checkForClass() { classCheck = $('p').is('.example-class'); document.querySelector('.output').textContent = classCheck; } </script> </body> </html> |
Output:

Recommended Posts:
- How to check if an element is hidden in jQuery?
- How to check an element is visible or not using jQuery?
- How to check an element has certain CSS style using jQuery ?
- How to check an HTML element is empty using jQuery ?
- How to find the parent class name with known class in jQuery ?
- How to remove parent element except its child element using jQuery ?
- jQuery | .class Selector
- Not class selector in jQuery
- jQuery | Move an element into another element
- How to change the element id using jQuery ?
- jQuery | element + next Selector
- How to create a div element in jQuery ?
- jQuery | element Selector
- jQuery | Create a div element
- How to select an element by name with jQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



