Search code snippets, questions, articles...

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
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

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?