Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Use 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.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Check a specific element
Select the element with jQuery, then call .hasClass() with the class name:
#1 Best Overall
- 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
- 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.
Quick Recap
Best Value
$('.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.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()returnsfalse.
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.




