-
list = new ClassList(element)
Constructor. Create a new ClassList
object. new
is optional.
var ClassList = require('classlist')
var list = new ClassList(element)
var classList = require('classlist')
var list = classList(element)
-
list.item(index)
or list[index]
Returns the class at the given index
. Example:
var div = document.createElement('div')
div.className = 'a b'
var list = classList(div)
list.item(0)
list[1]
-
list.contains(class)
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.
When force
is true, the class is always added (i.e. it is equivalent to calling add
). When force
is 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.toggle('not_empty', counter > 0)
if (counter > 0) {
list.add('not_empty')
} else {
list.remove('not_empty')
}
Returns true
if the class is now present, or false
otherwise.
-
list.replace(oldClass, newClass)
If oldClass
is present on the element, it is replaced by newClass
. If oldClass
is not present, this method has no effect.
Returns true
if the class was replaced, false
otherwise.
This module has been tested for compatibility with IE8+, and may work in even older browsers. If it doesn't, open an issue.