how to add class using js to a element code example
Example 1: js add class
const div = document.createElement('div');
div.className = 'foo';
console.log(div.outerHTML);
div.classList.remove("foo");
div.classList.add("anotherclass");
console.log(div.outerHTML);
div.classList.toggle("visible");
div.classList.toggle("visible", i < 10 );
console.log(div.classList.contains("foo"));
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);
div.classList.replace("foo", "bar");
Example 2: javascript add class to element
var someElement= document.getElementById("myElement");
someElement.className += " newclass";