jQuery Class Selector (“.class”)

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
CSS class match

What You’ll Learn

The class selector .class matches every element that includes a given CSS class — regardless of tag name. Available since jQuery 1.0; one of the most common selectors in real projects for styling hooks, state flags, and component markup.

01

Syntax

.myClass

02

Any tag

div, span, …

03

Official

.myClass demo

04

AND

.a.b

05

Scope

#panel .x

06

Native

getElementsByClassName

Introduction

CSS classes group elements for shared styling and behavior. In HTML you write class="myClass"; in jQuery you target them with $(".myClass"). Unlike ID selectors (one element per page in valid HTML), the same class can appear on many elements — making classes ideal for buttons, cards, alerts, and reusable components.

Official jQuery documentation notes that an element may carry several class names at once (class="card featured"). A single class selector matches if any one of those tokens equals your search string. jQuery also optimizes simple class queries with the browser’s native getElementsByClassName() when available.

Understanding the Class Selector

The dot prefix tells jQuery you are searching by class name, not tag or ID:

  • class="myClass" on a div → matches $(".myClass").
  • class="myClass" on a span → also matches — tag does not matter.
  • class="box myClass highlight" → still matches $(".myClass").
  • class="other" → no match for $(".myClass").
💡
Beginner Tip

To require two classes on the same element, write them back-to-back: $(".myclass.otherclass"). Do not put a space between them — a space means descendant, not AND.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( ".class" )



// common patterns:

$( ".myClass" )

$( ".myclass.otherclass" )   // must have BOTH classes

$( "#sidebar .nav-link" )    // scoped by parent ID

$( "button.primary" )        // tag + class combined

Parameters

  • class — a class name to search for (without the leading dot in HTML, with the dot in the selector string).

Return value

  • A jQuery object containing every element whose class attribute includes the given class token.
  • Empty collection when no elements match.

Official jQuery API — Example 1

jQuery
// Finds every element with class "myClass" (div and span)

$( ".myClass" ).css( "border", "3px solid red" );

Official jQuery API — Example 2

jQuery
// Element must have BOTH "myclass" AND "otherclass"

$( ".myclass.otherclass" ).css( "border", "3px solid red" );

⚡ Quick Reference

HTML class attribute$(".myClass")
class="myClass"Matches
class="box myClass"Matches (one token is enough)
class="other"No match
class="myclass otherclass" with $(".myclass.otherclass")Matches (both required)
class="myclass" only with $(".myclass.otherclass")No match (missing otherclass)

📋 Class vs ID, Tag, and Methods

Pick the right selector or method for the job.

Class
.myClass

Many elements OK

ID
#myId

One unique element

AND classes
.a.b

Both on same node

.hasClass()
el.hasClass("x")

Test one element

Examples Gallery

Examples 1 and 2 follow the official jQuery API demos. Examples 3–5 cover scoped selection, counting matches, and toggling visibility by class. Use the Try-it links to run each snippet.

📚 Official jQuery Demos

Border every matching element — any tag with the target class.

Example 1 — Official Demo: $(".myClass")

Official jQuery example 1 — add a red border to every element with class myClass, whether it is a div or span.

jQuery
$( ".myClass" ).css( "border", "3px solid red" );



// div.myClass and span.myClass both match — tag name ignored
Try It Yourself

How It Works

The class selector ignores element type. Any node whose class list includes myClass is collected into one jQuery object for chaining.

Example 2 — Official Demo: $(".myclass.otherclass")

Official jQuery example 2 — match only elements that have both classes on the same node.

jQuery
$( ".myclass.otherclass" ).css( "border", "3px solid red" );



// No space between .myclass and .otherclass — means AND, not descendant
Try It Yourself

How It Works

Chained class selectors filter to elements whose class attribute contains every listed token. This is different from $(".myclass .otherclass"), which would mean “otherclass inside myclass.”

📈 Practical Patterns

Scoped queries, counting, and interactive UI state classes.

Example 3 — Scoped Selection: $("#panel .active")

Find active items only inside one container — ignore matching classes elsewhere on the page.

jQuery
$( "#panel .active" ).addClass( "highlight" );



// Only .active elements inside #panel — not #sidebar .active
Try It Yourself

How It Works

Prefix with an ID or parent class to narrow the search root. This pattern is essential on pages with repeated component markup.

Example 4 — Count Matches: $(".tag").length

Display how many tag chips exist — read .length on the jQuery collection.

jQuery
var count = $( ".tag" ).length;

$( "#tagCount" ).text( count + " tag(s) on this page" );
Try It Yourself

How It Works

Class selectors often return multiple nodes. .length gives a plain number without looping — useful for badges, analytics, and validation.

Example 5 — Toggle Visibility: $(".hidden")

Show every element marked with a utility hidden class when the user clicks a button.

jQuery
$( "#showAll" ).on( "click", function() {

  $( ".hidden" ).removeClass( "hidden" ).fadeIn();

});



// Select by class to find; .removeClass() to change state
Try It Yourself

How It Works

State classes like hidden, active, or selected are a common pattern. Select with .class, then mutate with class methods.

🚀 Common Use Cases

  • Component styling — target all .card or .btn-primary elements.
  • State flags — select .active, .disabled, or .loading nodes.
  • Scoped tabs$("#tabs .tab-pane.active") inside one widget.
  • Validation errors — highlight every .field-error input wrapper.
  • Filter chips — count or hide .tag labels dynamically.
  • AND filtering$(".product.featured") for compound class rules.

🧠 How jQuery Evaluates .class

1

Parse selector

jQuery reads .myClass as a class token search — optionally combined with tags, IDs, or combinators.

Syntax
2

Native lookup

Simple class-only queries use getElementsByClassName() when the browser supports it.

Fast path
3

Match tokens

Compare against space-separated class names on each element’s class attribute.

DOM
4

Return collection

All matching elements become a jQuery object ready for chaining.

📝 Notes

  • Available since jQuery 1.0 — standard CSS class selector syntax.
  • An element can have multiple classes; one matching token is enough for .singleClass.
  • Chained classes (.a.b) require every listed class on the same element.
  • Class names are case-sensitive — match HTML exactly.
  • jQuery uses getElementsByClassName() for simple class selectors when supported.
  • Use .addClass(), .removeClass(), and .toggleClass() to change classes — not the selector alone.

Browser Support

The .class selector is standard CSS and works in jQuery 1.0+. All modern browsers support document.getElementsByClassName() and document.querySelectorAll(".myClass"). jQuery has provided consistent class matching across browsers for over a decade.

CSS · jQuery 1.0+

jQuery Class Selector (".class")

Supported in all modern browsers. Native: getElementsByClassName("myClass"). Case-sensitive class token matching.

100% Standard CSS
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: Prefer meaningful class names and scope with #id or parent selectors. Use .a.b for AND, not .a .b.

Conclusion

The class selector .class is one of the first jQuery patterns beginners learn — and one of the most used in production. The official $(".myClass") demo shows that tag type does not matter; chained $(".myclass.otherclass") shows how to require multiple classes on the same element.

Scope with parent selectors, count with .length, and pair selection with .addClass() / .removeClass() when you need to change state rather than find existing markup.

💡 Best Practices

✅ Do

  • Use semantic class names (.nav-link, .card-title)
  • Scope with #container .active on complex pages
  • Chain .a.b when both classes must be present
  • Match exact casing from your HTML
  • Use .hasClass() to test a single known element

❌ Don’t

  • Confuse .a.b (AND) with .a .b (descendant)
  • Rely on generic names like .red or .big alone
  • Assume classes are case-insensitive
  • Use classes for unique one-off elements — prefer IDs
  • Duplicate styling logic in JS when CSS already handles it

Key Takeaways

Knowledge Unlocked

Five things to remember about the class selector

Match by class token — any tag, many elements.

5
Core concepts
tag 02

Any tag

div or span

Rule
.a.b 03

AND

Both classes

Pattern
# 04

Scope

#panel .x

Tip
native 05

Fast

getElementsByClassName

Perf

❓ Frequently Asked Questions

The class selector .class matches every element whose class attribute includes that class name. $(".myClass") finds divs, spans, buttons, or 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 would mean descendant, not AND — $(".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. Complex selectors still go through the Sizzle engine.
$(".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").
Did you know?

Before querySelectorAll was everywhere, jQuery’s optimized class lookups via getElementsByClassName made $(".btn") feel instant even on older browsers — one reason class selectors became the default way to wire up interactive UIs.

Continue to Multiple Selector

Combine tag, class, and ID selectors with comma OR logic — the official div, span, p.myClass demo.

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.

6 people found this page helpful