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.

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

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

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

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

Join the Lean Web Club! Coaching. Courses. Coding resources. Get the skills, confidence, and support you need to learn front-end web development and achieve long-term success.


Find this useful? You can support my work by purchasing an annual membership.