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
Fundamentals
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.
Concept
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.
Foundation
📝 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" );
Cheat Sheet
⚡ 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)
Compare
📋 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
Hands-On
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
class="myclass otherclass" → red border
class="myclass" only → no border
class="otherclass" only → no border
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
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
Click "Show hidden" → all .hidden paragraphs fade in
Class selector finds; .removeClass() updates state
How It Works
State classes like hidden, active, or selected are a common pattern. Select with .class, then mutate with class methods.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.classUniversal
Bottom line: Prefer meaningful class names and scope with #id or parent selectors. Use .a.b for AND, not .a .b.
Wrap Up
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.
Use classes for unique one-off elements — prefer IDs
Duplicate styling logic in JS when CSS already handles it
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the class selector
Match by class token — any tag, many elements.
5
Core concepts
.01
.class
By name
Syntax
tag02
Any tag
div or span
Rule
.a.b03
AND
Both classes
Pattern
#04
Scope
#panel .x
Tip
native05
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.