The .addClass() method appends one or more CSS class names to each matched element — it never replaces existing classes. This tutorial covers all four API overloads (string, array, callback, callback returning array), comparisons with .removeClass(), .toggleClass(), and .css(), and practical tab-toggle patterns.
01
String
.addClass("x")
02
Multiple
Space or array
03
Callback
Per element
04
vs remove
Strip classes
05
vs css
Classes vs inline
06
Since 1.0
Core API
Fundamentals
Introduction
Styling in modern web pages usually lives in CSS files — reusable class names like .active, .error, or .hidden that you toggle on elements at runtime. When JavaScript needs to turn a style on, jQuery provides .addClass().
Available since jQuery 1.0, .addClass() updates the class attribute on each matched element. It appends new class names without removing what is already there — unlike setting element.className directly, which would wipe existing classes. For inline property values, use .css() instead.
Concept
Understanding the .addClass() Method
Given a jQuery object representing one or more DOM elements, .addClass() adds the specified class name(s) to each element’s class attribute. Styles defined in your stylesheet for those classes take effect immediately — no need to set individual CSS properties in JavaScript.
Think of classes as labels you stick onto elements. .addClass() adds more labels; .removeClass() peels them off; .toggleClass() flips a label on or off. This keeps presentation in CSS and behavior in JavaScript — a clean separation of concerns.
💡
Beginner Tip
$("p").last().addClass("selected") adds selected to the last paragraph only — every other class on that element stays untouched.
Foundation
📝 Syntax
jQuery .addClass() accepts four argument forms:
1. String — one or more class names (since 1.0)
jQuery
.addClass( className )
className — a string of one or more space-separated CSS class names to add to each matched element.
2. Array — list of class names (since 3.3)
jQuery
.addClass( [ classNames ] )
classNames — an array of strings; jQuery adds every entry to each element.
📋 .addClass() vs .removeClass() vs .toggleClass() vs .css()
Four styling approaches — append classes, strip classes, flip classes, or set inline properties.
.addClass()
+ class
Append class names — keeps existing classes
.removeClass()
- class
Strip one, some, or all classes
.toggleClass()
on/off
Add if missing, remove if present
.css()
inline
Set style properties directly on the element
Hands-On
Examples Gallery
Examples 1–4 follow the official jQuery API documentation. Example 5 shows a practical tab toggle. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demos for adding CSS classes.
Example 1 — Official Demo: Add selected to Last Paragraph
Highlight the final paragraph by appending the selected class.
li 0 → class "item-0" (red tint)
li 1 → class "item-1" (green tint)
li 2 → class "item-2" (blue tint)
How It Works
jQuery invokes the callback once per matched element. The returned string becomes the class name to add. Since 3.3 the callback can return an array of class names instead.
📈 Practical Patterns
Combine .removeClass() and .addClass() for exclusive state switches.
Example 5 — Tab Toggle With .removeClass() and .addClass()
Clear active from all tabs, then add it to the clicked tab — a common UI pattern.
Clicked tab → .active added (blue background)
Previously active tab → .active removed
Only one tab styled as active at a time
How It Works
.removeClass("active") strips the state from every tab; .addClass("active") on $(this) marks only the clicked one. You could use .toggleClass() for simple on/off, but remove-then-add guarantees exactly one active tab.
Applications
🚀 Common Use Cases
Active navigation item — $("nav a").removeClass("active"); $(this).addClass("active").
Form validation errors — $("input:invalid").addClass("error") to apply error styling from CSS.
Show/hide with CSS — $(".panel").addClass("visible") where .visible { display: block; }.
Animation hooks — $("div").addClass("animate-in") to trigger CSS transitions or keyframes.
Delegated styling — $(this).parent().addClass("active-group") after a child click (see .parent() tutorial).
🧠 How .addClass() Updates Elements
1
Match elements
jQuery object holds one or more DOM nodes to update.
Input
2
Resolve class names
String, array, or callback produces class name(s) per element.
Parse
3
Update class attribute
New names append to existing classes — nothing removed.
DOM
4
🎨
Styles apply
CSS rules for new classes take effect — return same jQuery object for chaining.
Important
📝 Notes
Available since jQuery 1.0 — core class manipulation API.
Does not replace existing classes — only appends new class names.
Array argument supported since jQuery 3.3; callback since 1.4.
Callback receives (index, currentClass) — return string or array (3.3+).
SVG and XML elements supported since jQuery 1.12.
Returns the original jQuery object for chaining — e.g. .addClass("x").fadeIn().
Compatibility
Browser Support
.addClass() has been part of jQuery since 1.0+. Array arguments arrived in 3.3+; SVG/XML support in 1.12+. It relies on standard class attribute manipulation with no browser-specific quirks beyond jQuery itself.
✓ jQuery 1.0+
jQuery .addClass()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.classList.add() — jQuery adds collection semantics, callback forms, and older-browser consistency.
100%With jQuery loaded
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
.addClass()Universal
Bottom line: Safe in any jQuery project. Prefer .addClass() for stylesheet classes; use .css() for one-off inline values.
Wrap Up
Conclusion
The jQuery .addClass() method appends one or more CSS class names to each matched element. It is the standard way to turn stylesheet-defined styles on at runtime — without touching inline properties or wiping existing classes.
Remember the official demo: $("p").last().addClass("selected") highlights one paragraph while preserving its other classes. Chain .removeClass() when switching exclusive states, reach for .toggleClass() for simple on/off, and use .css() only when you need dynamic inline values.
Define reusable styles in CSS and toggle them with .addClass()
Chain .removeClass() before .addClass() for exclusive states
Use the array form when class names come from a JavaScript array (3.3+)
Use the callback form when each element needs a unique class
Keep class names semantic — .active, .error, not .red-text
❌ Don’t
Expect .addClass() to remove or replace existing classes
Use .addClass() for one-off pixel values — use .css() instead
Add duplicate classes repeatedly in a loop without checking — jQuery deduplicates, but it is wasteful
Mix too many single-purpose classes — consolidate in your stylesheet
Forget that .toggleClass() may be simpler for binary on/off states
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .addClass()
Append classes — never replace.
5
Core concepts
+01
.addClass()
Append
API
−02
.removeClass()
Strip
Compare
⇄03
.toggleClass()
Flip
Compare
[]04
Array
3.3+
Syntax
fn05
Callback
Per item
1.4+
❓ Frequently Asked Questions
.addClass() appends one or more CSS class names to each element in the current jQuery collection. It updates the element's class attribute so stylesheet rules for those classes apply — without changing HTML content or inline style properties.
No. .addClass() only adds new class names alongside whatever is already on the element. To remove old classes first, chain .removeClass() or use .toggleClass() when you want on/off behavior.
.addClass() appends class names for stylesheet-driven styling. .css() sets inline style properties. .removeClass() strips classes. .toggleClass() adds a class when missing and removes it when present — useful for switches without a separate remove step.
Yes. Pass a space-separated string — addClass('selected highlight') — or since jQuery 3.3 an array — addClass(['selected', 'highlight']). jQuery adds every listed class to each matched element.
Since jQuery 1.4, you can pass a function: addClass(function(index, currentClass) { return 'item-' + index; }). jQuery calls it once per element and adds the returned string. Since 3.3 the callback may return an array of class names.
Yes, since jQuery 1.12 .addClass() supports SVG and XML elements in addition to HTML. Class manipulation on SVG follows the same API — useful for toggling states on icons and vector graphics.
Did you know?
jQuery’s .addClass() has existed since version 1.0 — one of the earliest DOM manipulation methods. The callback form (1.4+) lets you generate class names per element without a manual loop, and since 1.12 it works on SVG elements too — so you can add .highlighted to an icon path the same way you would a <div>.