Files
30-seconds-of-code/snippets/addClass.md
2022-05-03 13:35:33 +03:00

23 lines
528 B
Markdown

---
title: Add class to HTML element
tags: browser
expertise: beginner
author: chalarangelo
cover: blog_images/budapest-palace.jpg
firstSeen: 2020-12-30T19:21:15+02:00
lastUpdated: 2020-12-30T19:21:15+02:00
---
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
```