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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
check-class-hasClass

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
check-class-is



My Personal Notes arrow_drop_up

Technical Writer Lets have an update

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.