javascript get element class name code example

Example 1: javascript element by class

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

Example 2: javascript get class name

document.getElementById("myDIV").className = "mystyle";

Example 3: how get class name

String className = this.getClass().getSimpleName();

Example 4: html get class property

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

Example 5: element.classname javascript

let el = document.getElementById('item');

if (el.className === 'active'){
  el.className = 'inactive';
} else {
  el.className = 'active';
}

Tags:

Java Example