How to check an element contains a class using jQuery? Last Updated : 23 Apr, 2019 Comments Improve Suggest changes Like Article Like Report 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: html <!DOCTYPE html> <html> <head> <title> How to check an element contains a class using jQuery? </title> <script src= "https://code.jquery.com/jquery-3.3.1.min.js"> </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: html <!DOCTYPE html> <html> <head> <title> How to check an element contains a class using jQuery? </title> <script src= "https://code.jquery.com/jquery-3.3.1.min.js"> </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: Comment More infoAdvertise with us Next Article How to check an element contains a class using jQuery? S sayantanm19 Follow Improve Article Tags : Web Technologies JQuery jQuery-Misc Similar Reads How to check an element contains specific class in jQuery ? In this article, we will see how to check if an element contains a specific class using jQuery. The Jquery function called hasClass() function will be used, which will return a boolean result if the particular element contains any specific class. Note that we can check for multiple classes available 3 min read How to check an element has certain CSS style using jQuery ? Given an HTML document containing some CSS property and the task is to check whether an element has a specific CSS style or not with the help of jQuery. Approach 1: Use css() method to check an element contains certain CSS styles or not. Example: This example uses css() method to check an element co 2 min read How to check an HTML element is empty using jQuery ? Given an HTML document and select an element from the HTML document and check that element is empty or not. There are two methods used to solve this problem which are discussed below: Method 1: Using the ":empty" selector: The element to be checked using is() method. The is() method is used to check 3 min read How to fire event if CSS class is changed using jQuery ? Given an HTML document with heading and button, jQuery should fire an event when the CSS class of the button changes. Approach: In jQuery, there is no provision to trigger an event on class change. An alternative method is to manually raise an event when you programmatically change the class using t 2 min read How to find the class of clicked element using jQuery ? In this article, we will find the class of the clicked element using jQuery. To find the class of clicked element, we use this.className property. The className property is used to set or return the value of an elementâs class attribute. Using this property, the user can change the class of an eleme 1 min read Like