.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")
Select elements, pass one class name, branch on the boolean.
Select the element(s) with jQuery.
Call .hasClass("name") with a single class name (no leading dot).
Use the true / false result in an if or variable.
To test one element in a set, narrow first with .first() or .eq(n).
Piece
Required?
Role
jQuery collection
Yes
Element(s) to inspect
className
Yes
One 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)
Reference
Cheat Sheet
Keep these facts handy while checking classes.
Topic
Detail
Syntax
.hasClass(className)
Since
jQuery 1.2
Return
true | false
Match rule
True if any matched element has the class
Multiple names
One name per call — combine with &&
Dot?
No — use "active", not ".active"
SVG / XML
Supported since jQuery 1.12 / 2.2
Try it
Live Preview
Run the official demo checks against two sample paragraphs (selected on the second only).
First
Second
Browser demo of .hasClass() — same idea as the examples below.
Result
$("p").first().hasClass("selected")
→ false
Code
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.
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.”
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Analysis
Performance Notes
Approach
Cost
Best for
.hasClass("x")
Low
Simple class presence checks
.is(".x")
Similar / higher
Full selector tests
classList.contains
Lowest
Hot 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.
Remember
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.