The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element. This can then be used to manipulate the class list.
Below is the one liner example on how to add / remove class:
//Add class
document.getElementById("elementId").classList.add("classToBeAdded");
//Remove class
document.getElementById("elementId").classList.remove("classToBeRemoved");
You can also add or remove multiple classes
element = document.getElementById("elementId")
element.classList.add("foo", "bar", "baz");
element.classList.remove("foo", "bar", "baz");
The classList.toggle() method in JavaScript allows you to toggle a CSS class on an HTML element.
- If the element already has the specified class, classList.toggle() removes it.
// if visible is set remove it, otherwise add it
div.classList.toggle("visible");
Refrence