Files
30-seconds-of-code/snippets/toggleClass.md
Isabelle Viktoria Maciohsek 5cb69e3c5c Update snippet descriptions
2020-10-22 20:24:30 +03:00

18 lines
403 B
Markdown

---
title: toggleClass
tags: browser,beginner
---
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
```