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

Recommended Posts:
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | prepend() with Examples
- jQuery | dblclick() with Examples
- jQuery | offset() with Examples
- jQuery | children() with Examples
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- jQuery | focusin() with Examples
- jQuery | Animation, Slide methods with Examples
- jQuery | change() with Examples
- jQuery | filter() with Examples
- jQuery | height() and innerHeight() with Examples
- jQuery | eq() with Examples
- jQuery | empty() with Examples
- jQuery | not() method with Examples
- jQuery | first() with Examples
- jQuery | parent() & parents() with Examples
- jQuery | noConflict() with Examples
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.



