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

526 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Toggle class of HTML element browser blog_images/laptop-plants-2.jpg 2017-12-28T23:46:33+02:00 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.
const toggleClass = (el, className) => el.classList.toggle(className);
toggleClass(document.querySelector('p.special'), 'special');
// The paragraph will not have the 'special' class anymore