HTML Element .classList
| Since: | ES6(ECMAScript 2015) |
|---|
A property for manipulating the class attribute of an HTML element. It provides methods to add, remove, toggle, and check for the existence of classes.
Syntax
// Adds a class.
element.classList.add("className");
// Removes a class.
element.classList.remove("className");
// Removes the class if it exists, or adds it if it does not.
element.classList.toggle("className");
// Checks whether a class exists.
var result = element.classList.contains("className");
Method List
| Method | Description |
|---|---|
| add("className") | Adds the specified class. If the class already exists, nothing happens. You can also add multiple classes at once by separating them with commas. |
| remove("className") | Removes the specified class. Specifying a class that does not exist does not cause an error. You can also remove multiple classes at once by separating them with commas. |
| toggle("className") | Removes the specified class if it exists, or adds it if it does not. Useful for toggling menus open/closed or switching active states. |
| contains("className") | Returns true or false indicating whether the specified class exists. |
Sample Code
sample_classList.html
<div id="card" class="card">Kiryu Kazuma — Dojima Family</div> <button id="btn-highlight">Toggle Highlight</button> <button id="btn-reset">Reset</button>
sample_classList.js
var card = document.getElementById("card");
// Pattern 1: Add a class
card.classList.add("active");
console.log(card.className); // Outputs "card active"
// Pattern 2: Add multiple classes at once
card.classList.add("rounded", "shadow");
console.log(card.className); // Outputs "card active rounded shadow"
// Pattern 3: Check whether a class exists
console.log(card.classList.contains("active")); // Outputs "true"
console.log(card.classList.contains("hidden")); // Outputs "false"
// Pattern 4: Remove a class (no error if the class does not exist)
card.classList.remove("shadow");
console.log(card.classList.contains("shadow")); // Outputs "false"
// Pattern 5: Toggle a class on button click (removes if present, adds if absent)
var btnHighlight = document.getElementById("btn-highlight");
btnHighlight.addEventListener("click", function() {
card.classList.toggle("highlight");
});
// Pattern 6: Switch classes based on a condition
var isActive = card.classList.contains("active");
if (isActive) {
card.classList.remove("active");
card.classList.add("inactive");
}
Overview
element.classList is a property for safely and easily manipulating the class attribute of an HTML element. Previously, you had to directly manipulate a string using the element.className property, but the methods on element.classList make it straightforward to add and remove individual classes.
The toggle() method in particular is handy for switching UI states. Operations such as opening and closing menus, switching active tabs, or toggling dark mode — any "on/off" state switch — can be written in a single line.
Because add() and remove() do not throw an error when you add or remove a class that does not exist, there is no need to check for the class beforehand.
Browser Compatibility
21 ×
20 △
19 △
18 △
17 △
16 △
13 △
12 △
11 △
10 △
9 △
8 △
7 or earlier ×
3.5 or earlier ×
6 or earlier ×
11 or earlier ×
Android Browser
37+ ○
4 ×
3.4 △
3 △
2.4 or earlier ×
Chrome Android
36+ ○
24 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.