Skip to main content Accessibility Feedback


Unless otherwise noted, these work in all modern browsers, and IE9+. You can extend support back further with polyfills.


Add, remove, toggle, and check for the presence of a class.

var elem = document.querySelector('#sandwich');

// Add a class

// Remove a class

// Toggle a class
// (Add the class if it's not already on the element, remove it if it is.)

// Check if an element has a specific class
if (elem.classList.contains('mayo')) {
	console.log('add mayo!');

Works back to IE10. Extend back to IE8 with a polyfill.


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.