Snippets

Melodi Pace addClass

Created by Melodi Pace

File addClass.md Added

  • 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
+```
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.