Definition and Usage
The classList.remove() method removes the specified class attribute value from a DOM element.
- You can remove a single class value.
- You can remove multiple class values at once.
- If the class value you want to remove does not exist, no action is taken.
Note
Strictly speaking, it is not removing a value from the class attribute, but rather removing a class name from 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.remove() should technically be described as removing a class name from the list of class names that is the value of the element's class attribute, for consistency, this document uses the expression "remove a class value".
Syntax
element.classList.remove(className1)
// You can remove multiple class values at once.
element.classList.remove(className1, className2)
element.classList.remove(className1, className2, /*...,*/ classNameN)
The remove() function is a method of the classList object.
Parameters
className |
Required. The class value to remove from the element (as a string).
You can remove multiple class values separated by commas ( ,). |
|---|
Return Value
None (undefined).
Examples
Removing a Single Class Value
The following example removes 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");
});
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 "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.
Removing Multiple Class Values at Once
The following example removes multiple class values from an element at once.
<style>
.bold {
font-weight: 600;
}
.bg-red {
background-color: red;
}
.color-white {
color: white;
}
</style>
<p class="bold bg-red color-white">Clicking the button removes multiple class values.</p>
<button type="button">Remove a Class Value from the Element</button>
To remove multiple class values, write the class values to remove 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
// Remove multiple class values separated by commas (,)
elem.classList.remove("bg-red", "color-white");
});
"Remove a Class Value from the Element" button!
Clicking the button removes multiple class values.
Removing Multiple Class Values at Once - Using the Spread Syntax (...)
You can use the spread syntax (...) to remove multiple class values at once.
const classes = ["a", "b", "c"];
element.classList.remove(...classes);
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
classList.remove()
|
24 | 12 | 26 | 7 |
Specifications
| Specification | |
|---|---|
classList.remove()
|
DOM Standard #ref-for-dom-domtokenlist-remove① |
See Also
- JavaScript classList Object – Manipulating the Class Attribute of DOM Elements
- JavaScript classList.add() – Add Class Values to an Element
- JavaScript classList.contains() – Check Class Values of an Element
- JavaScript removeAttribute() Function – Removing Specified Attributes from Elements
- JavaScript classList.toggle() – Add a Class Value If It Does Not Exist, or Remove It If It Does
- JavaScript toggleAttribute() Function – Toggling a Boolean Attribute on an Element