Files
30-seconds-of-code/snippets/toggleClass.md
2022-12-04 22:20:49 +02:00

21 lines
526 B
Markdown

---
title: Toggle class of HTML element
tags: browser
cover: blog_images/laptop-plants-2.jpg
firstSeen: 2017-12-28T23:46:33+02:00
lastUpdated: 2020-10-22T20:24:30+03:00
---
Toggles a class for an HTML element.
- Use `Element.classList` and `DOMTokenList.toggle()` to toggle the specified class for the element.
```js
const toggleClass = (el, className) => el.classList.toggle(className);
```
```js
toggleClass(document.querySelector('p.special'), 'special');
// The paragraph will not have the 'special' class anymore
```