DomClassList
This class encapsulates a list of CSS classes which can be set as the className
on an HTMLElement.
Properties names set on this class equate to adding a class if the property's value is truthy, or removing a class if the value is falsy.
const myClassList = new DomClassList('b-test-button');
myClassList.add('test-class');
myClassList.important = 1;
myHtmlElement.className = myClassList; // Sets it to "b-test-button test-class important"
Properties
2
Properties
2Functions
13
Functions
13Add CSS class(es)
myClassList.add('bold', 'small');
| Parameter | Type | Description |
|---|---|---|
classes | String | String[] | Object | CSS classes to add |
Adds/removes class names according to the passed object's properties.
Properties with truthy values are added. Properties with falsy values are removed.
| Parameter | Type | Description |
|---|---|---|
classList | Object | Object containing properties to set/clear |
Adds/removes this objects classes to the passed classList or element.
Properties with truthy values are added. Properties with falsy values are removed.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | DOMTokenList | The element or the element's |
Clears all class names from this DomClassList instance.
this DomClassList.
Returns a clone of this DomClassList with all the same keys set.
A clone of this DomClassList.
Initializes a new DomClassList.
| Parameter | Type | Description |
|---|---|---|
classes | String | Object | The CSS classes as strings or objects. |
Returns a Boolean value, indicating whether this ClassList has the specified CSS class name.
| Parameter | Type | Description |
|---|---|---|
className | String | CSS class name to check |
true if this ClassList contains the passed CSS class name, false otherwise
Compares this DomClassList to another DomClassList (or class name string of space separated classes). If the same class names (regardless of order) are present, the two are considered equal.
So new DomClassList('foo bar bletch').isEqual('bletch bar foo') would return true
| Parameter | Type | Description |
|---|---|---|
other | DomClassList | String | The |
true if the two contain the same class names.
Remove CSS class(es)
myClassList.remove('bold', 'small');
| Parameter | Type | Description |
|---|---|---|
classes | String | CSS classes to remove |
Sets this DomClassList instance to represent the classes passed as either strings or objects.
| Parameter | Type | Description |
|---|---|---|
classes | String | The CSS classes to set. |
this DomClassList.
Analogous to the String#split method, but with no delimiter
parameter. This method returns an array containing the individual
CSS class names set.
The individual class names in this DomClassList
Toggles the passed CSS class name.
If the force parameter is passed, true means add the class name, false means remove it.
myClassList.toggle('bold', isImportant);
| Parameter | Type | Description |
|---|---|---|
className | String | CSS class to toggle |
force | Boolean |
|
true if the operation changed the value.
Analogous to string.trim, returns the string value of this DomClassList with no trailing space.
A concatenated string value of all the class names in this DomClassList
separated by spaces.