jQuery Class Selector

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .class?

The class selector $(".class") (since jQuery 1.0) selects every element whose class attribute includes that name. An element can have many classes — only one must match. It is a standard CSS selector; jQuery uses native getElementsByClassName() when available.

Remember
$(".myClass")              // any tag with class myClass
$("div.myClass")           // only divs with that class
$(".myclass.otherclass")   // both classes required (no space!)
$(".a .b")                 // .b inside .a (descendant)

// Change classes with .addClass() / .removeClass()
// Test with .hasClass("myClass")
NeedUse
Find by class$(".myClass")
Tag + class$("div.myClass")
Both classes$(".a.b")
Add a class.addClass("x")
Test a class.hasClass("x")
Find by id$("#id")

How to Use .class

Put a dot before the class name, then optionally narrow by tag or parent.

  1. Write $(".className") with the exact casing from your HTML.
  2. Prefix with a tag when you only want certain elements: $("span.myClass").
  3. Chain classes with no space for AND: $(".card.featured").
  4. Scope under a parent when the page is large: $("#panel .item").
  5. To change classes, use .addClass() / .removeClass() — the selector only finds.
PieceRequired?Role
. + class nameYesClass to match (one of possibly many on the element)
Tag prefixOptionalLimits matches to that element type
Extra .classOptionalRequires additional classes on the same element
Return value—jQuery collection of matching elements

Pseudocode

Pseudocode
matches = []
for each element in document (or context):
    classList = element.class attribute split by spaces
    if targetClass is in classList:
        matches.add(element)

return matches

// $(".a.b") → targetClass A AND targetClass B both in classList
// Native path: getElementsByClassName(targetClass) when possible

Cheat Sheet

Keep these facts handy while selecting by class.

TopicDetail
Syntax$(".className")
SincejQuery 1.0 · CSS standard
Multi-class HTMLOnly one listed class must match
AND classes$(".a.b") — no space
Descendant$(".a .b") — with space
CaseCase-sensitive in HTML
Native APIUses getElementsByClassName when possible
Empty setNo match → length 0

Live Preview

Toggle which boxes have myClass, then run $(".myClass") and see what matches.

Sample elements

Same idea as the official jQuery demo — any tag with myClass can match.

Result
$(".myClass").length
→ 2

matched → #box1 (div), #box3 (span)

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Official $(".myClass")

Find every element that includes the class myClass.

jQuery
var labels = [];
$(".myClass").each(function () {
  labels.push(this.tagName.toLowerCase() + "#" + (this.id || "(no-id)"));
});

$("#out").text(
  "Matched: " + $(".myClass").length + "\n" +
  labels.join(", ")
);
Try It Yourself

How It Works

  1. Query. $(".myClass") scans for that class name.
  2. Any tag. Both the div and the span match.
  3. Skip. class="notMe" does not match.
  4. List. We print tag names so you can see what was selected.

Example 2 — Both Classes Required

Official idea: $(".myclass.otherclass") needs both classes on the same element.

jQuery
$("#out").text(
  "Only myclass: " + $(".myclass").length + "\n" +
  "Both myclass + otherclass: " + $(".myclass.otherclass").length
);
Try It Yourself

How It Works

  1. Single. $(".myclass") matches every element that has that class.
  2. AND. $(".myclass.otherclass") keeps only elements with both.
  3. No space. A space would mean descendant, not “both classes”.
  4. Use case. Great for states like .card.active.

Example 3 — Tag + Class

Limit the match to one element type with div.myClass.

jQuery
$("#out").text(
  "Any .myClass: " + $(".myClass").length + "\n" +
  "Only div.myClass: " + $("div.myClass").length
);
Try It Yourself

How It Works

  1. Broad. $(".myClass") finds divs and spans.
  2. Narrow. $("div.myClass") keeps only matching divs.
  3. Why. Tag prefixes make intent clearer and reduce accidental matches.
  4. Same idea. Works with span., button., li., and so on.

Example 4 — Scope Under a Parent

Search for a class only inside one container so other regions stay untouched.

jQuery
var n = $("#panel .item").length;
var all = $(".item").length;

$("#out").text(
  "Inside #panel: " + n + "\n" +
  "Whole page .item: " + all
);
Try It Yourself

How It Works

  1. Scope. #panel .item only looks under #panel.
  2. Global. Bare $(".item") also finds items outside the panel.
  3. Space. Here the space means descendant — intentional.
  4. Habit. Prefer scoped selectors on large pages.

Example 5 — Select vs .hasClass()

Use the selector to find elements; use .hasClass() to test one element.

jQuery
var found = $(".active").length;
var firstIsActive = $("#card").hasClass("active");

$("#out").text(
  "$('.active') count: " + found + "\n" +
  "$('#card').hasClass('active'): " + firstIsActive
);
Try It Yourself

How It Works

  1. Find. $(".active") returns a collection of matches.
  2. Test. .hasClass("active") returns a boolean for one element.
  3. Change. To toggle the class, use .addClass() / .removeClass().
  4. Rule. Selector = search; method = inspect or mutate.

Edge Cases & Pitfalls

Check these before relying on class selectors in real UIs.

space

.a.b vs .a .b

No space = both classes on one element. With a space = descendant. Mixing them up is the most common class-selector bug.

case

Class names are case-sensitive

$(".MyClass") will not match class="myclass" in HTML. Copy the exact spelling from your markup.

scope

Broad $(".item") hits the whole page

Reusable class names appear everywhere. Prefer $("#section .item") or a tag prefix when you mean a local set.

mutate

Selector does not add classes

$(".active") only finds. To change class lists, use .addClass(), .removeClass(), or .toggleClass().

Browser Support

The .class selector is standard CSS and has been in jQuery since 1.0. Class-only queries use native getElementsByClassName() when the browser provides it.

✓ Stable · CSS · jQuery 1.0+

jQuery .class

Supported across jQuery 1.x, 2.x, and 3.x in every browser that runs your jQuery build. Prefer current jQuery 3.x for modern projects.

100% Class selector support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$(".class") Universal

Bottom line: Safe everywhere jQuery runs. Prefer scoped or tagged class queries on large pages for clarity and speed.

Performance Notes

ApproachCostBest for
$(".class")Cheap (native)Simple class lookups
$("div.class")CheapTag-filtered matches
$("#id .class")Cheapest scopedLarge pages / reusable class names
$(".a .b .c") deepHigherOnly when deep nesting is required

Official docs note that class-only selectors can use getElementsByClassName(). Keep selectors simple and scoped when class names are reused across the page.

Key Takeaways

  • Role: $(".class") finds elements that include that class.
  • AND: use $(".a.b") — never a space — for both classes.
  • Scope: prefer $("#box .item") or $("div.item") on busy pages.
  • Mutate: change classes with .addClass() / .toggleClass().

One line: $(".class") finds by class — chain without spaces for AND, scope when class names are reused.

Frequently Asked Questions

The class selector .class matches every element whose class attribute includes that class name. $(".myClass") finds any tag with class="myClass", or class="foo myClass bar" where myClass is one of several classes.
Chain class names with no space: $(".myclass.otherclass") matches only elements that have both classes. A space means descendant — $(".a .b") selects .b inside .a, not an element with both classes.
In HTML documents, class names are case-sensitive. $(".MyClass") does not match class="myclass". Match the exact spelling and casing used in your HTML.
Yes. For class-only selectors, jQuery uses the browser’s native getElementsByClassName() when supported — fast and well optimized. More complex selectors still go through Sizzle.
$(".active") selects elements that already have the class. .addClass("active") adds the class to elements in an existing jQuery collection. Select to find; .addClass() to change.
Yes. Official jQuery docs: an element can have multiple classes; only one of them must match. class="card featured myClass" still matches $(".myClass").

Next: Multiple Selector

Learn how to combine several selectors in one query with commas.

Multiple selector tutorial →

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.

8 people found this page helpful