get classes js code example

Example 1: javascript get element by class

var elementsArray = document.getElementsByClassName("someclassname");

Example 2: js add class

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

Example 3: Javascript change element class

document.getElementById("myElementID").classList.add('myClassName');
document.getElementById("myElementID").classList.remove('myClassName');

Example 4: add multiple class list at once in js

elem.classList.add("first");
elem.classList.add("second");
elem.classList.add("third");

is equal to :

elem.classList.add("first","second","third");

Example 5: js get class property

let element = document.querySelector('.preset');
let style = getComputedStyle(element);
let bg = style.backgroundColor;