Element.classList.add
Adds the given tokens to the element's class list
Syntax
element.classList.add(...tokens)Parameters
| Parameter | Type | Description |
|---|---|---|
| tokens | ...string[] | One or more class names to add |
Return Value
undefined
Examples
const el = document.querySelector('.box')!
el.classList.add('active')
el.classList.add('visible', 'animated')function highlight(id: string) {
const el = document.getElementById(id)
el?.classList.add('highlight', 'pulse')
}document.querySelectorAll('tr:nth-child(even)').forEach(row => {
row.classList.add('striped')
})Understanding Element.classList.add
The Element.classList.add method in JavaScript adds the given tokens to the element's class list. It belongs to the DOMTokenList object and is one of the most widely used methods for working with domtokenlist values in modern JavaScript and TypeScript applications.
The method signature is element.classList.add(...tokens). It accepts 1 parameter: tokens. When called, it returns undefined. Understanding when and how to use add() helps you write more expressive, readable code.
Common use cases for Element.classList.add include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-classlist-remove, dom-classlist-toggle, dom-classlist-contains, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Element.classList.add is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Element.classList.removeRemoves the given tokens from the element's class list
Element.classList.toggleToggles a class name in the element's class list: removes it if present, adds it if not
Element.classList.containsReturns a boolean indicating whether the element's class list contains the given class
More DOMTokenList Methods
Other methods in the DOMTokenList object
Related Tools
More DOMTokenList Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.