Definition and Usage
The classList is an object that allows you to easily manipulate the class attribute of DOM elements.
It provides several useful methods and properties for adding, removing, replacing, and checking class values.
Note
The class attribute can have multiple space-separated class values.
Strictly speaking, the value of the class attribute is a list of space-separated class names. For consistency, this document uses the expression "class value" to refer to the value of the class attribute depending on the context.
Syntax
element.classList
Additional Explanation
element is a DOM element object.
classList is a read-only property of element and returns an object that allows you to easily manipulate the class attribute.
Therefore, in this document, classList is referred to as an "object" for easier understanding of its functionality.
Methods and Properties of the classList Object
The classList object provides several methods and properties for easily manipulating the class attribute of DOM elements.
Methods
The classList object includes the following methods.
classList.add()
|
Adds the specified class value to a DOM element. |
|---|---|
classList.remove()
|
Removes the specified class value from a DOM element. |
classList.toggle()
|
Adds the specified class value to a DOM element if it does not exist, and removes it if it does. |
classList.replace() |
Replaces an existing class value in a DOM element with a new class value. |
classList.contains()
|
Checks whether the specified class value exists in a DOM element. |
Properties
The classList object provides the following properties.
classList.length |
Returns the number of class values contained in the element's list of class names. |
|---|---|
classList.value |
eturns the class value of the element as a string.
If multiple class values exist, they are returned as a single space-separated string. |
Examples
The following examples demonstrate the methods and properties of the classList object.
classList.add()
The classList.add() method adds the specified class value to a DOM element.
You can add a single class value or multiple class values at the same time.
The following example demonstrates adding 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.
classList.remove()
The classList.remove() method removes the specified class value from a DOM element.
You can remove a single class value or multiple class values at the same time.
The following example demonstrates removing a single class value from an element.
<style>
.bold {
font-weight: 600;
}
.bg-red {
background-color: red;
}
</style>
<p class="bold bg-red">Clicking the button removes the "bg-red" class value.</p>
<button type="button">Remove a Class Value from the Element</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
// Remove the "bg-red" class value
elem.classList.remove("bg-red");
});
Clicking the "Remove a Class Value from the Element" button removes the "bg-red" class value from the <p> element. (The red background color is removed from the element.)
"Remove a Class Value from the Element" button!
Clicking the button removes the "bg-red" class value.
classList.toggle()
The classList.toggle() method adds the specified class value to a DOM element if it does not exist, and removes it if it does.
<style>
.bold {
font-weight: 600;
}
.bg-red {
background-color: red;
}
</style>
<p class="bold">The background color is added or removed.</p>
<button type="button">Add/Remove Background Color</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
// Adds the "bg-red" class value if it does not exist; otherwise removes it
elem.classList.toggle("bg-red");
});
Adding the "bg-red" class value to the <p> element with the classList.toggle() method adds a red background color. Conversely, removing it removes the background color.
"Add/Remove Background Color" button!
The background color is added or removed.
classList.replace()
The classList.replace() method replaces an existing class value in a DOM element with a new class value.
The following example replaces the bg-red class value of the <p> element with bg-blue when the button is clicked.
<style>
.bold {
font-weight: 600;
color: white;
}
.bg-red {
background-color: red;
}
.bg-blue {
background-color: blue;
}
</style>
<p class="bold bg-red">The background color changes when the button is clicked.</p>
<hr>
<button type="button">Replace Class Value</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
// Replaces the "bg-red" class value with "bg-blue" if it exists
elem.classList.replace("bg-red", "bg-blue");
});
"Replace Class Value" button!
The background color changes when the button is clicked.
classList.contains()
The classList.contains() method checks whether the specified class value exists in a DOM element.
It returns true if the specified class value exists in the element, and false otherwise.
<video src="video.mp4" class="a b c"></video>
const myVideo = document.querySelector("video");
console.log(myVideo.classList.contains("a")); // true
console.log(myVideo.classList.contains("b")); // true
console.log(myVideo.classList.contains("d")); // false
classList.length
The classList.length property returns the number of class values contained in the element's list of class names.
<div class="one two three">This is a div element.</div>
const elem = document.querySelector("div");
const classCount = elem.classList.length;
console.log("Number of class values: " + classCount);
// "Number of class values: 3"
classList.value
The classList.value property returns the class value of the element as a string.
If multiple class values exist, they are returned as a single space-separated string.
<div class="one two three">This is a div element.</div>
const elem = document.querySelector("div");
const classValue = elem.classList.value;
console.log(classValue);
// "one two three"
Browser Compatibility
| Interface |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
classList
|
22 | 16 | 3.6 | 7 |
add()
|
24 | 12 | 26 | 7 |
remove()
|
24 | 12 | 26 | 7 |
toggle()
|
24 | 12 | 24 | 7 |
replace()
|
67 | 18 | 61 | 12 |
contains()
|
8 | 12 | 3.6 | 5.1 |
length
|
8 | 12 | 3.6 | 5.1 |
value
|
50 | 17 | 47 | 10 |
Specifications
| Specification | |
|---|---|
classList
|
DOM Standard #ref-for-dom-element-classlist① |
References
See Also
- JavaScript getAttribute() Function – Getting Attribute Values from Elements
- JavaScript setAttribute() Function – Setting or Adding Attributes to Elements
- JavaScript removeAttribute() Function – Removing Specified Attributes from Elements
- JavaScript hasAttribute() Function – Checking If an Element Has an Attribute
- JavaScript toggleAttribute() Function – Toggling a Boolean Attribute on an Element