Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

jQuery | hasClass() with Examples

  • Last Updated : 13 Feb, 2019

The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not.
Syntax:

$(selector).hasClass(className);

Parameter: It accepts a “className” parameter which specifies the class name need to search in the selected element.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Return Value: It returns true if the search is successful otherwise false.
jQuery code to show the working of this method:




<html>
  
<head>
    <script 
    </script>
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                alert($("p").hasClass("find"));
            });
        });
    </script>
    <style>
        .find {
            font-size: 120%;
            color: green;
        }
          
        body {
            width: 50%;
            height: 200px;
            border: 2px solid green;
            padding: 20px;
        }
    </style>
</head>
  
<body>
  
    <h1>Heading 1</h1>
  
    <p class="find">Geeks for Geeks !.</p>
    <p>This is normal paragraph.</p>
  
    <button>Click me!</button>
  
</body>
  
</html>

Output:
Before clicking the “Click me!” button-

After clicking the “Click me!” button-




My Personal Notes arrow_drop_up
Recommended Articles
Page :