JavaScriptDOM and events

Element.classList

An object for adding, removing, toggling, and checking an element’s CSS classes.

Returns
A DOMTokenList object. The toggle(), contains(), and replace() methods return true or false.
On this page

Example

#
JavaScript
const card = document.querySelector('#card')

card.classList.add('active', 'highlight')
card.classList.remove('highlight')
console.log(card.className)

console.log(card.classList.toggle('done'))
console.log(card.classList.contains('done'))
console.log(card.classList.toggle('done'))
console.log(card.className)
Console
card active
true
true
false
card active

Definition and usage

#

The classList property returns a DOMTokenList object with the element’s classes and methods for changing them. It’s the most convenient way to change a page’s appearance from JavaScript: you describe the style in CSS under a class name, and the script only adds or removes it.

The key methods are add() and remove(), which accept one or more classes, toggle(), which adds a class when it’s missing and removes it when it’s present, contains() for checking, and replace(oldClass, newClass). toggle() returns true if the class was added, and a second argument (true or false) lets you force the outcome.

Unlike overwriting className, the classList methods leave the other classes alone. Adding a class that’s already there, or removing one that isn’t, doesn’t break anything.

Syntax

#
Syntax
element.classList.add(className1, className2, …)
element.classList.remove(className1, className2, …)
element.classList.toggle(className)
element.classList.toggle(className, force)
element.classList.contains(className)
element.classList.replace(oldClass, newClass)

Parameters

#
  • className

    string

    A class name without the dot, e.g. "active".
  • force

    optional, boolean

    In toggle(): true always adds the class, and false always removes it.
  • oldClass, newClass

    strings

    In replace(): the class to replace, and the class that takes its place.

More examples

#
A theme switch
JavaScript
const panel = document.querySelector('#panel')
const button = document.querySelector('#theme')

button.addEventListener('click', () => {
  const isLight = panel.classList.toggle('light')
  console.log(isLight ? 'Light theme' : 'Dark theme')
})

button.click()
Console
Light theme
Forcing a state with the second argument
JavaScript
const email = document.querySelector('#email')
const isValid = email.value.includes('@') && email.value.includes('.')

email.classList.toggle('invalid', !isValid)

console.log(email.classList.contains('invalid'))
console.log([...email.classList])
Console
true
['invalid']

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Pass class names without the dot: classList.add("active"), not ".active".
  • Describe appearance in CSS, and only toggle classes in JavaScript. The code is more readable than setting many styles through style.
  • Assigning element.className = "active" removes every other class, while classList.add() adds a class alongside the existing ones.

Related entries

#

Spotted a mistake or missing an example? Write to us.