Javascript examples for jQuery Method and Property:hasClass
The hasClass() method checks if selected elements have a specified class name.
Parameter | Require | Description |
---|---|---|
classname | Required. | class name to search for in the selected elements |
The following code shows how to Check if any <p> element has a class named "intro":
<!DOCTYPE html> <html> <head> <style> .intro {//from www . j ava 2s . c o m font-size: 120%; color: red; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ console.log($("p").hasClass("intro")); }); }); </script> </head> <body> <h1>This is a heading</h1> <p class="intro">This is a paragraph.</p> <p>This is another paragraph.</p> <button>Does any p element have an "intro" class?</button> </body> </html>