Snippets
Created by
Melodi Pace
- Title: addClass
- Tags: browser,beginner
- design inspiration
Adds a class to an HTML element.
- Use
Element.classList
andDOMTokenList.add()
to add the specified class to the element.
const addClass = (el, className) => el.classList.add(className);
addClass(document.querySelector('p'), 'special');
// The paragraph will now have the 'special' class
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.