Files
30-seconds-of-code/snippets/addClass.md
2020-12-30 19:21:15 +02:00

364 B

title, tags
title tags
addClass browser,beginner

Adds a class to an HTML element.

  • Use Element.classList and DOMTokenList.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