jQuery hasClass() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

In jQuery, the hasClass() method is used to check if a selected element has a specific CSS class applied to it or not. jQuery is a popular JavaScript library that simplifies HTML document traversal, event handling, and manipulation.

The hasClass() method is particularly useful when you want to perform conditional actions based on whether an element has a certain class assigned to it. It helps us determine if a specific class exists within the set of classes applied to an HTML element.

Syntax

Certainly, here's the syntax for the hasClass() method in jQuery.

The $() function in jQuery is a versatile selector used to manipulate DOM elements using various selection criteria. It grants access to the jQuery library and returns a jQuery object representing selected element(s). With this object, the hasClass() method can be employed to determine if the chosen element(s) possess a particular CSS class. The method takes a class name as a string input, devoid of a preceding dot, and evaluates whether the class is present among the element's classes.

Return Value: Boolean

The hasClass() method returns a boolean value (true or false) based on whether the selected element(s) have the specified class.

If any of the selected elements have the class we're checking for, the method returns true. If none of the selected elements have the class, the method returns false.

Sharpen Your Fundamentals with Free Learning

Parameters

The hasClass() method takes a single parameter, which is the name of the CSS class we want to check for on the selected element(s). Here's a more detailed breakdown of the parameters.

  • className (required)

    The className parameter is a string value that should match the name of the CSS class exactly. It's important to note that you provide the class name without a leading dot (.). The method will then check whether the selected element(s) have this class applied.

Here's an example of how the hasClass() method is used with its parameter.

Suppose we have an HTML element like this

We can use the hasClass() method to check if the element has the class "highlight" using the following code.

Explanation:

The $("#myElement") selector targets the element with the ID "myElement". The hasClass("highlight") method checks if the element has the class "highlight". Depending on the result, the appropriate message will be logged to the console.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Examples

Here are some basic examples of using the hasClass() method in jQuery with simple code snippets and their expected outputs.

Example 1: Check if an Element has a Class

Code:

Output:

Explanation:

The above code demonstrates how to use jQuery's hasClass() method to determine if a specific HTML element has a particular CSS class. It creates a <div> element with the class "highlight" and then uses jQuery to check if it contains the class. The script logs a message confirming whether the element has the "highlight" class. In this case, since the class is applied, the output indicates that the element indeed has the 'highlight' class.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 2: Toggle Class on Button Click

Output:

Before: toggle class on button click before

After: toggle class on button click after

Similar to the previous jQuery example, when the "Toggle Active" button is clicked, the paragraph's font weight changes to bold and its color to green. Clicking the button again removes these styles.

Explanation:

The code showcases how to replicate the behavior of jQuery's toggleClass() using pure JavaScript. A button labeled "Toggle Active" and some paragraphs are present in the HTML. When the button is clicked, the active class is alternated on the paragraphs. JavaScript's addEventListener() is used to attach a click event to the button. Upon clicking, the script iterates through the paragraphs and toggles the active class using classList.toggle(), leading to the toggling of font weight and color styles. This provides a similar outcome to jQuery's class toggling.

Conclusion

  • The hasClass() method in jQuery is used to check if an HTML element has a specific CSS class applied to it. It's often used for conditional actions or styling based on the presence or absence of a class.

  • The hasClass() method helps in dynamically manipulating and styling elements based on their class status.

  • The hasClass() method takes one parameter: the name of the class to be checked. The class name is provided as a string without the leading dot (.).