Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk1 min

How to Check Whether an Element Has a Class in jQuery

Use jQuery’s .hasClass() method with a class name—without a leading period—to check whether a selected element has that class.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use jQuery’s .hasClass() method and pass the class name without a leading dot:

if ($('#myElement').hasClass('active')) {
  // The selected element has the "active" class.
}

How .hasClass() works

.hasClass(className) returns true if at least one element in the selected jQuery collection has the specified class; otherwise it returns false. Pass the class name as a string, without the CSS selector’s leading period.

As an Amazon Associate I earn from qualifying purchases.

const hasActiveClass = $('#myElement').hasClass('active');

That makes it useful in an if condition or anywhere else a Boolean result is needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check a specific element

Select the element with jQuery, then call .hasClass() with the class name:

#1 Best Overall
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
if ($('#myElement').hasClass('active')) {
  $('#myElement').text('Active');
} else {
  $('#myElement').text('Inactive');
}

The selector identifies the element; 'active' is the class being checked. Do not write .hasClass('.active'): the argument is a class name, not a CSS selector.

Check a collection of elements

If the jQuery selection matches multiple elements, .hasClass() returns true when any one of them has the class. It does not mean every matched element has it.

Rank #2
Sale
JavaScript and jQuery: Interactive Front-End Web Development
  • JavaScript Jquery
  • Introduces core programming concepts in JavaScript and jQuery
  • Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
if ($('.item').hasClass('selected')) {
  // At least one .item is selected.
}

To test each matched element individually, call the method on each element in an iteration:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 1
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05
SaleBestseller No. 2
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript Jquery; Introduces core programming concepts in JavaScript and jQuery; Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
$22.75
$('.item').each(function () {
  if ($(this).hasClass('selected')) {
    // This particular item is selected.
  }
});
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes

  • Including a leading period: use .hasClass('active'), not .hasClass('.active').
  • Assuming all matches have the class: for a collection, a true result means at least one matched element has it.
  • Checking an empty selection: if the selector matches no elements, .hasClass() returns false.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.