jQuery addClass() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

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

Syntax

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

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

// Does NOT replace — only appends missing names
CallWhat it does
addClass()Learn jQuery .addClass(): append one or more CSS class names to matched elements, string array and callback forms, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select the element(s) with a jQuery selector.
Step 2Call .addClass("name") — or a space-separated list / array.
Step 3Optional: use a function when each element needs a different class.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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() Method — Check CSS Classes

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

hasClass() Method — Check CSS Classes 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.

9 people found this page helpful