Skip to main content Accessibility Feedback

Classes

Element.classList

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

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

// Add a class
elem.classList.add('turkey');

// Remove a class
elem.classList.remove('tuna');

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

// 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.