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
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️