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.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 28/01/2021
3176 People Used
Only 97 Left
Rating
5.0