javascript add class
snippet in javascript

js add class

user117

var element = document.getElementById('element');
element.classList.add('class-1');
element.classList.add('class-2', 'class-3');
element.classList.remove('class-3');

addclass javascript

user2877

var element = document.getElementById("myDIV");
// to add
element.classList.add("mystyle");
// to remove
element.classList.remove("mystyle");

javascript add class

user5213

// getting the target element
var myElement = document.querySelector('#myElement');

// example for addding some-class to the element
myElement.classList.add('some-class');

// multiple classes can be added like this
myElement.classList.add('one-class', 'one-more-class');

// example for removing any class from the element
myElement.classList.remove('any-class');

how to add class to button in javascript

user9016

var element = document.getElementById("myDIV");
element.classList.add("mystyle"); 

how to add a class to html using javascript

user7957

function myFunction() {
  var element = document.getElementById("myDIV");
  element.classList.add("mystyle");
}

javascript add class to element

user1931

var someElement= document.getElementById("myElement");
    someElement.className += " newclass";//add "newclass" to element (space in front is important)