jQuery hasClass() Method

Last Updated:

jQuery hasClass() Method

The jQuery hasClass() method determines whether any of the matched elements are assigned the given class.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> .point{ color: red; font-size: 22px; } </style> </head> <body> <h1>jQuery hasClass() Method</h1> <p class="point">Click on the button.</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ alert($("p").hasClass("point")); }); }); </script> </body> </html>

Syntax

$(selector).hasClass(className);

Parameter Values

ValueTypeExplanation
className Required Specifies one or more class to check whether it is used in the selected element.

Return Value

ValueExplanation
true Given class is used in the selected element
false Given class is NOT used in the selected element

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
hasClass()YesYesYesYesYesYesYesYesYesYesYesYesYes

Share this Page

Meet the Author