Add class to an element in javascript

var element = document.getElementById("element_id");
element.classList.add("className"); // ADD CLASS TO ELEMENT

element.classList.remove("className"); //REMOVE CLASS FROM ELEMENT

The code snippet describes how to add and re move class in javascript. You can simply use classList.add() to add class on an element. To remove class form an element you can use classList.remove() function.

Was this helpful?