18 lines
364 B
Markdown
18 lines
364 B
Markdown
---
|
|
title: addClass
|
|
tags: browser,beginner
|
|
---
|
|
|
|
Adds a class to an HTML element.
|
|
|
|
- Use `Element.classList` and `DOMTokenList.add()` to add the specified class to the element.
|
|
|
|
```js
|
|
const addClass = (el, className) => el.classList.add(className);
|
|
```
|
|
|
|
```js
|
|
addClass(document.querySelector('p'), 'special');
|
|
// The paragraph will now have the 'special' class
|
|
```
|