jQuery hasClass() Method

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

What Is hasClass()?

.hasClass(className) returns true if any matched element has that CSS class (since jQuery 1.2). It only reads the class list — it never changes the DOM.

Remember
$("#mydiv").hasClass("foo")   // true if class is present
$("#mydiv").hasClass("bar")   // true even if other classes exist
$("#mydiv").hasClass("quux")  // false if missing

// Set of elements → true if ANY match has the class
$("p").hasClass("selected")
NeedUse
Check one class$el.hasClass("active")
Add / remove / flip.addClass() / .removeClass() / .toggleClass()
Full selector test$el.is(".active")
Native checkel.classList.contains

How to Use hasClass()

Select elements, pass one class name, branch on the boolean.

  1. Select the element(s) with jQuery.
  2. Call .hasClass("name") with a single class name (no leading dot).
  3. Use the true / false result in an if or variable.
  4. To test one element in a set, narrow first with .first() or .eq(n).
PieceRequired?Role
jQuery collectionYesElement(s) to inspect
classNameYesOne class name string
Return—Boolean (not a jQuery object)

Pseudocode

Pseudocode
function hasClass(elements, className):
    for each element in elements:
        if element has className:
            return true
    return false

// Official idea:
$("p").first().hasClass("selected")  → false
$("p").last().hasClass("selected")   → true
$("p").hasClass("selected")          → true  (any match)

Cheat Sheet

Keep these facts handy while checking classes.

TopicDetail
Syntax.hasClass(className)
SincejQuery 1.2
Returntrue | false
Match ruleTrue if any matched element has the class
Multiple namesOne name per call — combine with &&
Dot?No — use "active", not ".active"
SVG / XMLSupported since jQuery 1.12 / 2.2

Live Preview

Run the official demo checks against two sample paragraphs (selected on the second only).

Browser demo of .hasClass() — same idea as the examples below.

Result
$("p").first().hasClass("selected")
→ false

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Official Demo Checks

First, last, and any paragraph for class selected.

jQuery
// <p>…</p><p class="selected">…</p>
console.log($("p").first().hasClass("selected")); // false
console.log($("p").last().hasClass("selected"));  // true
console.log($("p").hasClass("selected"));         // true
Try It Yourself

How It Works

  1. First. The opening paragraph has no selected → false.
  2. Last. The second paragraph has the class → true.
  3. Any. The full set includes that second paragraph → true.
  4. Read-only. Nothing in the DOM changed.

Example 2 — Element With Several Classes

One element can carry many classes; test each name separately.

jQuery
// <div id="mydiv" class="foo bar"></div>
var $div = $("#mydiv");
console.log($div.hasClass("foo"));  // true
console.log($div.hasClass("bar"));  // true
console.log($div.hasClass("quux")); // false
Try It Yourself

How It Works

  1. Space-separated. class="foo bar" means two class names.
  2. Each call. hasClass looks for one name at a time.
  3. Present. foo and bar both return true.
  4. Absent. quux is not on the element → false.

Example 3 — Any Match vs One Element

A collection returns true if any item matches; narrow to test one.

jQuery
// <li>A</li><li class="done">B</li><li>C</li>
console.log($("li").hasClass("done"));        // true (item B)
console.log($("li").eq(0).hasClass("done"));  // false (item A)
console.log($("li").eq(1).hasClass("done"));  // true  (item B)
Try It Yourself

How It Works

  1. Full set. One of three list items is done → true.
  2. eq(0). First item alone → false.
  3. eq(1). Second item alone → true.
  4. Tip. Use .eq() / .first() when “any” is too broad.

Example 4 — Branch With if

Use the boolean to choose add vs remove (or any other action).

jQuery
// <div id="card" class="highlight">Card</div>
var $card = $("#card");
if ($card.hasClass("highlight")) {
  $card.removeClass("highlight");
} else {
  $card.addClass("highlight");
}
console.log($card.hasClass("highlight")); // false
Try It Yourself

How It Works

  1. Read. hasClass("highlight") starts as true.
  2. Branch. The if path calls removeClass.
  3. Result. A second check returns false.
  4. Shortcut. For simple flips, .toggleClass() is shorter.

Edge Cases & Pitfalls

Check these before using .hasClass() in conditions.

chain

Not chainable

A Boolean comes back — $el.hasClass("x").addClass("y") throws. Split the calls.

dot

No CSS selector dot

Write hasClass("active"), not hasClass(".active").

multi

One name per call

Space-separated strings are not supported. Use && for two required classes.

any

Collections use “any”

One matching sibling makes the whole set true. Narrow the selection when you mean “this one.”

Browser Support

.hasClass() is a core jQuery method (since 1.2). It needs a browser DOM with jQuery loaded. SVG/XML support is solid since 1.12/2.2.

✓ jQuery 1.2+ · Browsers only

jQuery .hasClass()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Not for Node.js without a DOM.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.hasClass() Browsers

Bottom line: Use .hasClass("name") in the browser for boolean class checks. Remember: any-match on collections, one name per call.

Performance Notes

ApproachCostBest for
.hasClass("x")LowSimple class presence checks
.is(".x")Similar / higherFull selector tests
classList.containsLowestHot paths without jQuery

Prefer .hasClass() for a plain class name. Use .is() when you need a richer selector. Avoid calling it repeatedly inside tight loops over huge collections.

Key Takeaways

  • Role: .hasClass() → boolean class presence check.
  • Any match: true if one element in the set has the class.
  • One name: no space lists; combine with &&.
  • Read-only: does not change classes — pair with add/remove/toggle.

One line: ask .hasClass("name") for a boolean — any match wins, one class name per call.

Frequently Asked Questions

.hasClass(className) returns true if at least one matched element has that CSS class, otherwise false. It only reads — it does not add, remove, or toggle classes.
No. It returns a plain Boolean. You cannot chain jQuery methods after it — store the result or use it in an if statement.
Any. If even one element in the set has the class, the result is true. Narrow with .first(), .eq(), or a tighter selector to test one element.
No. Pass one class name only. To require two classes, use hasClass('foo') && hasClass('bar'). An element may still carry many classes at once.
.hasClass() tests one class name. .is() accepts a full selector (e.g. '.active', ':visible') and also returns Boolean. Prefer .hasClass() for a simple class check.
Yes, since jQuery 1.12/2.2 it supports SVG and XML elements as well as HTML.

Next: removeClass()

Learn how to strip CSS class names from matched elements.

removeClass() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful