jQuery hasClass() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

.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")
CallWhat 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

ParameterDescription
Step 1Select the element(s) with jQuery.
Step 2Call .hasClass("name") with a single class name (no leading dot).
Step 3Use the true / false result in an if or variable.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost 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.

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.

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.

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.

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() Method — Remove CSS Classes

Learn how to strip CSS class names from matched elements.

removeClass() Method — Remove CSS Classes tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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