jQuery addClass() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is addClass()?

.addClass(className) adds one or more CSS class names to each matched element (since jQuery 1.0). It updates the class attribute so stylesheet rules apply — without touching HTML text or inline styles.

Remember
$("p").addClass("selected")
$("p").addClass("selected highlight")     // multiple (space)
$("p").addClass(["selected", "highlight"]) // array (3.3+)

// Does NOT replace — only appends missing names
NeedUse
Turn a style on$el.addClass("active")
Turn a style off$el.removeClass("active")
Flip on/off$el.toggleClass("active")
Inline property$el.css("color", "red")

How to Use addClass()

Select elements, pass class names, then chain more jQuery if needed.

  1. Select the element(s) with a jQuery selector.
  2. Call .addClass("name") — or a space-separated list / array.
  3. Optional: use a function when each element needs a different class.
  4. Chain freely — .addClass() returns the same jQuery object.
PieceRequired?Role
jQuery collectionYesElements that receive the class(es)
className / array / functionYesWhat to append
Return—Same jQuery object (chainable)

Pseudocode

Pseudocode
for each matched element:
    names = resolve argument (string | array | function)
    for each class in names:
        if element does not already have that class:
            append it to the class attribute

// Official idea:
$("p").last().addClass("selected")
// last <p> gains "selected"; other classes stay

Cheat Sheet

Keep these facts handy while styling with classes.

TopicDetail
Syntax.addClass(className)
SincejQuery 1.0 (array form: 3.3+)
MultipleSpace string or ["a", "b"]
Callbackfunction(index, currentClass) since 1.4
DuplicatesAlready-present names are ignored
SVG / XMLSupported since jQuery 1.12 / 2.2
vs .css()Classes vs inline style properties

Live Preview

Pick a class string and run .addClass() on a sample paragraph. The result shows the updated class attribute.

Browser demo of .addClass() — same idea as the examples below.

Result
class before: "card"
$("p").addClass("selected")
class after:  "card selected"

jQuery Examples

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

Example 1 — Add One Class

Official API idea: add selected to the last paragraph.

jQuery
// <p>Hello</p><p>and</p><p>Goodbye</p>
$("p").last().addClass("selected");
console.log($("p").last().attr("class")); // "selected"
Try It Yourself

How It Works

  1. Select all. $("p") matches every paragraph.
  2. Narrow. .last() keeps only the final match.
  3. Append. .addClass("selected") writes that name onto the class attribute.
  4. Verify. Reading .attr("class") shows selected.

Example 2 — Multiple Classes (String)

Pass space-separated names to add several classes in one call.

jQuery
// <p>Hello</p><p>Goodbye</p>
$("p").last().addClass("selected highlight");
console.log($("p").last().attr("class")); // "selected highlight"
Try It Yourself

How It Works

  1. One string. "selected highlight" is two class names separated by a space.
  2. Both applied. jQuery adds each name to the matched element.
  3. Order. Names appear in the attribute roughly in add order.
  4. Safe to repeat. Calling again with the same names does not duplicate them.

Example 3 — Array Form (jQuery 3.3+)

Pass an array of class names — same result as a space-separated string.

jQuery
// <p>Hello</p><p>Goodbye</p>
$("p").last().addClass(["selected", "highlight"]);
console.log($("p").last().attr("class")); // "selected highlight"
Try It Yourself

How It Works

  1. Array overload. Available since jQuery 3.3.
  2. Same effect. Each string entry becomes a class on the element.
  3. When to use. Handy when class names already live in an array variable.
  4. Older jQuery. On 3.2 and below, join with spaces: names.join(" ").

Example 4 — Callback Per Element

Return a different class for each matched item using its index.

jQuery
// <ul><li>Alpha</li><li>Beta</li><li>Gamma</li></ul>
$("ul li").addClass(function (index) {
  return "item-" + index;
});
console.log($("ul li").map(function () {
  return $(this).attr("class");
}).get().join(", "));
// "item-0, item-1, item-2"
Try It Yourself

How It Works

  1. Per element. jQuery calls the function once for each <li>.
  2. Index. First arg is 0, 1, 2…
  3. Return value. The string you return is added as a class.
  4. Second arg. You also get currentClass if you need to branch on existing classes.

Edge Cases & Pitfalls

Check these before relying on .addClass() in UI code.

replace

It does not replace the class list

To wipe and set one class, use .attr("class", "only-this") — not .addClass() alone.

dot

Do not include the CSS dot

Write addClass("active"), not addClass(".active"). The leading dot is selector syntax only.

empty

Empty selection is a no-op

If the selector matches nothing, .addClass() quietly does nothing — no error.

legacy

Pre-1.12 used className only

Before jQuery 1.12/2.2, SVG/XML class updates were unreliable. Prefer a current jQuery for SVG icons.

Browser Support

.addClass() is a core jQuery CSS method (since 1.0). It needs a browser DOM with jQuery loaded — not Node.js without a DOM.

✓ jQuery 1.0+ · Browsers only

jQuery .addClass()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Array overload needs jQuery 3.3+. SVG/XML class updates solid since 1.12/2.2.

100% Browser 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
.addClass() Browsers

Bottom line: Use .addClass() in the browser with jQuery. Prefer 3.3+ if you pass class-name arrays.

Performance Notes

ApproachCostBest for
.addClass("x")LowUI state changes
.css({…})SimilarOne-off computed styles
Native classList.addLowestHot paths without jQuery

Prefer classes over many inline .css() calls — the browser can reuse stylesheet rules, and your JS stays shorter.

Key Takeaways

  • Role: .addClass() appends class names; it never wipes the list.
  • Many at once: space string or array (3.3+).
  • Callback: return per-element names with function(index, currentClass).
  • Pair: remove / toggle / css for the other styling jobs.

One line: use .addClass() to turn stylesheet states on — append only, never replace.

Frequently Asked Questions

.addClass() appends one or more CSS class names to each element in the current jQuery collection. Existing classes stay in place — it never replaces the whole class attribute.
No. It only adds names that are missing. To swap classes, chain .removeClass() then .addClass(), or use .toggleClass() for on/off flips.
Yes. Pass a space-separated string — addClass('selected highlight') — or since jQuery 3.3 an array — addClass(['selected', 'highlight']).
Since jQuery 1.4 you can pass a function(index, currentClass). jQuery calls it once per element and adds the returned class name(s). Since 3.3 the callback may return an array.
.addClass() updates the class attribute so stylesheet rules apply. .css() sets inline style properties. Prefer classes for reusable UI states; use .css() for one-off computed values.
Yes, since jQuery 1.12/2.2 .addClass() writes the class attribute (not only className), so it works on HTML, SVG, and XML elements.

Next: hasClass()

Learn how to check whether matched elements already have a CSS class.

hasClass() 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.

9 people found this page helpful