This module aims to match the behaviour of
Element#classList, without polyfilling it.
IE9 doesn't have the native
classList, and other browsers have incomplete implementations. This module tries to bridge the gaps.
npm install classlist (or
yarn add classlist)
var classList =var element = documentvar list =// Add a class:list// Remove a class:list// Toggle a class:list// Replace class 'foo' with 'bar'list// Check if the element has a class:if listconsole
This module tries to follow the native API as closely as possible.
list = new ClassList(element)
Constructor. Create a new
newis optional.// Object style, "new"var ClassList =var list = element// Function style, no "new"var classList =var list =
Returns the class at the given
index. Example:var div = documentdivclassName = 'a b'var list =list // 'a'list1 // 'b'
Returns true if the element has the given class; false otherwise.
list.add(class1 [, class2 [, ... ]])
Adds one or more classes to the element associated with
list. If the class is already present on the element, it is not added again.
list.remove(class1 [, class2 [, ... ]])
Removes one or more classes from the element, if present.
list.toggle(class [, force ])
Toggles the given class; removes the class if it is present, or adds the class if it is not present.
forceis true, the class is always added (i.e. it is equivalent to calling
forceis false, the class is always removed (i.e. it is equivalent to calling
remove). This can be useful when conditioning on an outcome; for example,list// is equivalent to:if counter > 0listelselist
trueif the class is now present, or
oldClassis present on the element, it is replaced by
oldClassis not present, this method has no effect.
trueif the class was replaced,
This module has been tested for compatibility with IE8+, and may work in even older browsers. If it doesn't, open an issue.