Skip to main content Accessibility Feedback

Classes

Element.className

Get all of the classes on an element as a string, add a class or classes, or completely replace or remove all classes.

var elem = document.querySelector('div');

// Get all of the classes on an element
var elemClasses = elem.className;

// Add a class to an element
elem.className += ' vanilla-js';

// Completely replace all classes on an element
elem.className = 'new-class';

Works back to IE6.