Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. HTML Element .classList

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

MethodDescription
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");
}

View sample page

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

Chrome Chrome
49+
21 ×
20
19
18
17
16
13
12
11
10
9
8
↑ Partial support
7 or earlier ×
Firefox Firefox
57+
3.5 or earlier ×
Safari Safari
18+
6 or earlier ×
Edge Edge
80+
15 ×
14
13
12
↑ Partial support
11 or earlier ×
IE IE
11
10
↑ Partial support
9 or earlier ×
Opera Opera
48+
11 or earlier ×
iOS Safari iOS Safari
18+
6 or earlier ×
Android Browser Android Browser
37+
4 ×
3.4
3
↑ Partial support
2.4 or earlier ×
Chrome Android Chrome Android
36+
24 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .