🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

jQuery .hasClass(): Check Whether an Element Has a Certain Class

Reading time 1 min
Published Jan 18, 2018
Updated Oct 2, 2019

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.