Created by Melodi Pace 2021-02-18 View revision File addClass.md Added Side-by-side diff More Ignore whitespace Hide word diff +--- +- Title: addClass +- Tags: browser,beginner +- [design inspiration](https://www.inspirationde.com/) +--- + +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 +```