jQuery hasClass: Main Tips
- The jQuery
.hasClass()
checks whether the selected element has the specified class or not. - It returns a boolean value.
Using .hasClass()
jQuery .hasClass()
determines whether elements have a class you specified. In the example below, the boolean value is returned after clicking a button:
Example
$("button").click(() => {
alert($("div").hasClass("introduction"));
});
To check if an element in jQuery has class, follow this syntax:
$(selector).hasClass(className);
- The selector is used to specify the elements to check.
- The
className
defines the class you will be looking for.
Note: .hasClass() jQuery will return true if an element has the specified class. Keep in mind it might have multiple classes.