javascript
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?
Similar Posts
- Add a CSS class to element if dark mode is enabled using Javascript
- Get all child elements of parent element in javascript
- Array.find() - get the very first element satisfy a condition Javascript
- Get Tag Name of an Element using Javascript
- Efficiently Remove the Last Element from an Array in JavaScript [6 Methods Explained]
- Javascript to find which DOM element has the focus
- Lodash - get the first element from an array