DOM Element.className Property
The className property gets or sets an element's class attribute as a single string. classList is usually easier to work with.
Description
The className property gets or sets the value of the element's class attribute as one space-separated string. Setting it replaces every class. To add, remove or toggle individual classes without affecting the others, use classList. (It's called className because class is a reserved word in JavaScript.)
Reading className gives the attribute exactly as written, including any extra spaces. For SVG elements, className returns an SVGAnimatedString object rather than a string, which is another reason to prefer classList or getAttribute('class') in code that may handle SVG.
Setting className = '' removes every class at once, which is simpler than removing them one by one. Toggling individual classes is safer with classList, because it doesn't disturb classes added by other code.
Syntax
element.className
element.className = valueValue
A string of the element's classes, separated by spaces.
Example
Browser Support
The following information will show you the current browser support for the Element.className property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





