| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since October 2017.
The read-only classList property of the Element interface contains a live DOMTokenList collection representing the class attribute of the element. This can then be used to manipulate the class list.
Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.
A DOMTokenList object representing the contents of the element's class attribute. If the class attribute is not set or empty, it returns an empty DOMTokenList, i.e., a DOMTokenList with the length property equal to 0.
Although the classList property itself is read-only in the sense that you can't replace the DOMTokenList object, you can still assign to the classList property directly, which is equivalent to assigning to its value property. You can also modify the DOMTokenList object using the add(), remove(), replace(), and toggle() methods.
const div = document.createElement("div");
div.classList = "foo"; // forwarded to classList.value
// our starting state: <div class="foo"></div>
console.log(div.outerHTML);
// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("another-class");
// <div class="another-class"></div>
console.log(div.outerHTML);
// if visible is set remove it, otherwise add it
div.classList.toggle("visible");
// add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10);
// false
console.log(div.classList.contains("foo"));
// add or remove multiple classes
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");
// add or remove multiple classes using spread syntax
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);
// replace class "foo" with class "bar"
div.classList.replace("foo", "bar");
| Specification |
|---|
| DOM # ref-for-dom-element-classlist |
This page was last modified on Nov 25, 2025 by MDN contributors.
ElementactiveViewTransitionariaActiveDescendantElementariaAtomicariaAutoCompleteariaBrailleLabelariaBrailleRoleDescriptionariaBusyariaCheckedariaColCountariaColIndexariaColIndexTextariaColSpanariaControlsElementsariaCurrentariaDescribedByElementsariaDescriptionariaDetailsElementsariaDisabledariaErrorMessageElementsariaExpandedariaFlowToElementsariaHasPopupariaHiddenariaInvalidariaKeyShortcutsariaLabelariaLabelledByElementsariaLevelariaLiveariaModalariaMultiLineariaMultiSelectableariaOrientationariaOwnsElementsariaPlaceholderariaPosInSetariaPressedariaReadOnlyariaRelevantariaRequiredariaRoleDescriptionariaRowCountariaRowIndexariaRowIndexTextariaRowSpanariaSelectedariaSetSizeariaSortariaValueMaxariaValueMinariaValueNowariaValueTextassignedSlotattributeschildElementCountchildrenclassListclassNameclientHeightclientLeftclientTopclientWidthcurrentCSSZoomcustomElementRegistryelementTimingfirstElementChildidinnerHTMLlastElementChildlocalNamenamespaceURInextElementSiblingouterHTMLpartprefixpreviousElementSiblingrolescrollHeightscrollLeftscrollLeftMaxscrollTopscrollTopMaxscrollWidthshadowRootslottagNameafter()animate()append()ariaNotify()attachShadow()before()checkVisibility()closest()computedStyleMap()getAnimations()getAttribute()getAttributeNames()getAttributeNode()getAttributeNodeNS()getAttributeNS()getBoundingClientRect()getClientRects()getElementsByClassName()getElementsByTagName()getElementsByTagNameNS()getHTML()hasAttribute()hasAttributeNS()hasAttributes()hasPointerCapture()insertAdjacentElement()insertAdjacentHTML()insertAdjacentText()matches()moveBefore()prepend()pseudo()querySelector()querySelectorAll()releasePointerCapture()remove()removeAttribute()removeAttributeNode()removeAttributeNS()replaceChildren()replaceWith()requestFullscreen()requestPointerLock()scroll()scrollBy()scrollIntoView()scrollIntoViewIfNeeded()scrollTo()setAttribute()setAttributeNode()setAttributeNodeNS()setAttributeNS()setCapture()setHTML()setHTMLUnsafe()setPointerCapture()startViewTransition()toggleAttribute()afterscriptexecuteanimationcancelanimationendanimationiterationanimationstartauxclickbeforeinputbeforematchbeforescriptexecutebeforexrselectblurclickcompositionendcompositionstartcompositionupdatecontentvisibilityautostatechangecontextmenucopycutdblclickDOMActivateDOMMouseScrollfocusfocusinfocusoutfullscreenchangefullscreenerrorgesturechangegestureendgesturestartgotpointercaptureinputkeydownkeypresskeyuplostpointercapturemousedownmouseentermouseleavemousemovemouseoutmouseovermouseupmousewheelMozMousePixelScrollpastepointercancelpointerdownpointerenterpointerleavepointermovepointeroutpointeroverpointerrawupdatepointerupscrollscrollendscrollsnapchangescrollsnapchangingsecuritypolicyviolationtouchcanceltouchendtouchmovetouchstarttransitioncanceltransitionendtransitionruntransitionstartwebkitmouseforcechangedwebkitmouseforcedownwebkitmouseforceupwebkitmouseforcewillbeginwheelAbortControllerAbortSignalAbstractRangeAttrCDATASectionCharacterDataCommentCustomEventDOMErrorDOMExceptionDOMImplementationDOMParserDOMTokenListDocumentDocumentFragmentDocumentTypeEventEventTargetHTMLCollectionMutationObserverMutationRecordNamedNodeMapNodeNodeIteratorNodeListProcessingInstructionQuotaExceededErrorRangeShadowRootStaticRangeTextTreeWalkerXMLDocumentXPathEvaluatorXPathExpressionXPathResultXSLTProcessorYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |