DOM: List/Add/Remove Class Attribute

By Xah Lee. Date: . Last updated: .

This page shows you how to list/add/remove a HTML/XML element's class attribute.

Use the following methods to change a class value of a element.

Example:

<div id="x48389" class="aaa bbb ccc"></div>
const xx = document.getElementById("x48389");

xx.classList;

xx.classList.add("hh");

xx.classList.remove("bbb");

xx.classList.contains("aaa");

xx.classList.toggle("aaa");

Browser Support

classList is supported by all browsers as of 2010-10.

Basic DOM How-To

  1. DOM: Basic DOM Methods
  2. Get Elements by ID, Tag, Name, Class, CSS Selector
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Element's Attribute Value
  8. Set Element's Attribute Value
  9. List/Add/Remove Class Attribute
  10. Add/Remove Event Handler
  11. DOM: Navigate DOM Tree
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon