+6011-2088 4110 admin@amz.com.my
Your IP: 216.73.216.198 [Columbus]
Read News/Blog Back to News/Blog List

Check If Class Name Exists Using jQuery

In jQuery, you can check whether an element has a certain class name. You can use it to perform specific actions based on the class name of an element. For example, from a list of hyperlinks, you can target only hyperlinks that belong to certain categories which you can identify using a class name.

The syntax to do this, using jQuery's .hasClass() method, is as follows:

$(selector).hasClass('className');

The className in the syntax shown above is the class name you are checking for, without the . prefix.

An example of using this .hasClass() method is as follows:

if ($('#myElement').hasClass('active')) {
    console.log('The element has the "active" class.');
} else {
    console.log('The element does not have the "active" class.');
}

This method works for any jQuery selector, so you can also use it with classes, IDs, tags, or more complex selectors. You'll be able to make your website development projects more dynamic and user-friendly using this .hasClass() method.

Check If Class Name Exists Using jQuery
AuthorNaim Zulkipli
Date15 January 2025
Share This Post:
Chat with Us! Chat with AMZ IT Solutions

Contact AMZ IT Solutions

Message / Enquiry:
Close This

Become an Affiliate of AMZ IT Solutions

By submitting this form, you agree to have your information stored and managed by AMZ IT Solutions, and to be contacted by AMZ IT Solutions for administration, marketing, and training purposes.

Close This