jQuery Class Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$(".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")
CallWhat it does
ClassLearn the jQuery class selector .class: match elements by CSS class, require multiple classes with .a.b, scope queries, official myClass demo, pitfalls, browser support, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Write $(".className") with the exact casing from your HTML.
Step 2Prefix with a tag when you only want certain elements: $("span.myClass").
Step 3Chain classes with no space for AND: $(".card.featured").

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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”.

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.

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 →

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.

8 people found this page helpful