.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.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("#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")
Call
What it does
hasClass()
Learn jQuery .hasClass(): test if matched elements have a CSS class, boolean return, any-match behavior, examples, pitfalls, and FAQs.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the element(s) with jQuery.
Step 2
Call .hasClass("name") with a single class name (no leading dot).
Step 3
Use the true / false result in an if or variable.
Return Value
Return Value
Description
Boolean
Often true or false depending on the match or state.
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
Example 1 — Official Demo Checks
First, last, and any paragraph for class selected.
.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.