Definition and Usage
The classList.add() method adds the specified class attribute value to a DOM element.
Note
Strictly speaking, it is not adding a value to the class attribute, but rather adding a class name to the list of class names, which is the value of the class attribute. The class attribute specifies one or more class names for an element, and its value is a list of class names separated by spaces. Although classList.add() should technically be described as adding a class name to the list of class names that is the value of the element's class attribute, for consistency, this document uses the expression "add a class value".
Syntax
element.classList.add(className1)
// You can add multiple class values at once.
element.classList.add(className1, className2)
element.classList.add(className1, className2, /*...,*/ classNameN)
The add() function is a method of the classList object.
Parameters
className |
Required. The class value to add to the element (as a string).
You can add multiple class values separated by commas ( ,). |
|---|
Return Value
None (undefined).
Examples
Adding a Single Class Value
The following example adds a single class value to an element.
<style>
.bold {
font-weight: 600;
}
.bg-red {
background-color: red;
}
</style>
<p class="bold">Clicking the button adds the "bg-red" class value.</p>
<button type="button">Add a Class Value to the Element</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
// Add the "bg-red" class value
elem.classList.add("bg-red");
});
Code Explanation
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element.
Clicking the "Add a Class Value to an Element" button adds the "bg-red" class value to the <p> element. (A red background color is added to the element.)
"Add a Class Value to an Element" button!
Clicking the button adds the "bg-red" class value.
Adding Multiple Class Values at Once
The following example adds multiple class values to an element at once.
<style>
.bold {
font-weight: 600;
}
.bg-red {
background-color: red;
}
.color-white {
color: white;
}
</style>
<p class="bold">Clicking the button adds multiple class values.</p>
<button type="button">Add a Class Value to an Element</button>
To add multiple class values, write the class values separated by commas (,). The order in which they are written does not matter.
const elem = document.querySelector("p");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
// Add multiple class values separated by commas (,)
elem.classList.add("bg-red", "color-white");
});
"Add a Class Value to an Element" button!
Clicking the button adds multiple class values.
Adding Multiple Class Values at Once - Using the Spread Syntax (...)
You can use the spread syntax (...) to add multiple class values at once.
const classes = ["a", "b", "c"];
element.classList.add(...classes);
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
classList.add()
|
24 | 12 | 26 | 7 |
Specifications
| Specification | |
|---|---|
classList.add()
|
DOM Standard #ref-for-dom-domtokenlist-add① |
See Also
- JavaScript classList Object – Manipulating the Class Attribute of DOM Elements
- JavaScript classList.remove() – Remove Class Values from an Element
- JavaScript classList.contains() – Check Class Values of an Element
- JavaScript classList.toggle() – Add a Class Value If It Does Not Exist, or Remove It If It Does
- JavaScript getAttribute() Function – Getting Attribute Values from Elements
- JavaScript setAttribute() Function – Setting or Adding Attributes to Elements
- JavaScript toggleAttribute() Function – Toggling a Boolean Attribute on an Element