How to add or remove a CSS class with vanilla Javascript?

In this snippet, we'll learn how to add or remove a CSS class with vanilla Javascript (Without any Javascript framework such as Jquery...)

Add:

const element = document.getElementById("elementId");
element.classList.add("className");
Copy this code


Remove:

element.classList.remove("className");
Copy this code


The className in the code above is Css class that you want to handle for an element has Id is elementId

That's it, so easily.