This module defines the core dojo DOM class API.
See the dojo/dom-class reference documentation for more information.
add
(node,classStr)
Defined by dojo/dom-class
Adds the specified classes to the end of the class list on the passed node. Will not re-apply duplicate classes.
Parameter | Type | Description |
---|---|---|
node | String | DOMNode |
String ID or DomNode reference to add a class string too |
classStr | String | Array |
A String class name to add, or several space-separated class names, or an array of class names. |
Add a class to some node:
require(["dojo/dom-class"], function(domClass){
domClass.add("someNode", "anewClass");
});
Add two classes at once:
require(["dojo/dom-class"], function(domClass){
domClass.add("someNode", "firstClass secondClass");
});
Add two classes at once (using array):
require(["dojo/dom-class"], function(domClass){
domClass.add("someNode", ["firstClass", "secondClass"]);
});
Available in dojo/NodeList
for multiple additions
require(["dojo/query"], function(query){
query("ul > li").addClass("firstLevel");
});
contains
(node,classStr)
Defined by dojo/dom-class
Returns whether or not the specified classes are a portion of the class list currently applied to the node.
Parameter | Type | Description |
---|---|---|
node | String | DOMNode |
String ID or DomNode reference to check the class for. |
classStr | String |
A string class name to look for. |
Returns: boolean
Do something if a node with id="someNode" has class="aSillyClassName" present
if(dojo.hasClass("someNode","aSillyClassName")){ ... }
remove
(node,classStr)
Defined by dojo/dom-class
Removes the specified classes from node. No contains()
check is required.
Parameter | Type | Description |
---|---|---|
node | String | DOMNode |
String ID or DomNode reference to remove the class from. |
classStr | String | Array | Optional
An optional String class name to remove, or several space-separated class names, or an array of class names. If omitted, all class names will be deleted. |
Remove a class from some node:
require(["dojo/dom-class"], function(domClass){
domClass.remove("someNode", "firstClass");
});
Remove two classes from some node:
require(["dojo/dom-class"], function(domClass){
domClass.remove("someNode", "firstClass secondClass");
});
Remove two classes from some node (using array):
require(["dojo/dom-class"], function(domClass){
domClass.remove("someNode", ["firstClass", "secondClass"]);
});
Remove all classes from some node:
require(["dojo/dom-class"], function(domClass){
domClass.remove("someNode");
});
Available in dojo/NodeList
for multiple removal
require(["dojo/query"], function(query){
query("ul > li").removeClass("foo");
});
replace
(node,addClassStr,removeClassStr)
Defined by dojo/dom-class
Replaces one or more classes on a node if not present. Operates more quickly than calling dojo.removeClass and dojo.addClass
Parameter | Type | Description |
---|---|---|
node | String | DOMNode |
String ID or DomNode reference to remove the class from. |
addClassStr | String | Array |
A String class name to add, or several space-separated class names, or an array of class names. |
removeClassStr | String | Array | Optional
A String class name to remove, or several space-separated class names, or an array of class names. |
require(["dojo/dom-class"], function(domClass){
domClass.replace("someNode", "add1 add2", "remove1 remove2");
});
Replace all classes with addMe
require(["dojo/dom-class"], function(domClass){
domClass.replace("someNode", "addMe");
});
Available in dojo/NodeList
for multiple toggles
require(["dojo/query"], function(query){
query(".findMe").replaceClass("addMe", "removeMe");
});
toggle
(node,classStr,condition)
Defined by dojo/dom-class
Adds a class to node if not present, or removes if present. Pass a boolean condition if you want to explicitly add or remove. Returns the condition that was specified directly or indirectly.
Parameter | Type | Description |
---|---|---|
node | String | DOMNode |
String ID or DomNode reference to toggle a class string |
classStr | String | Array |
A String class name to toggle, or several space-separated class names, or an array of class names. |
condition | Boolean | Optional
If passed, true means to add the class, false means to remove. Otherwise dojo.hasClass(node, classStr) is used to detect the class presence. |
Returns: Boolean
If passed, true means to add the class, false means to remove. Otherwise dojo.hasClass(node, classStr) is used to detect the class presence.
require(["dojo/dom-class"], function(domClass){
domClass.toggle("someNode", "hovered");
});
Forcefully add a class
require(["dojo/dom-class"], function(domClass){
domClass.toggle("someNode", "hovered", true);
});
Available in dojo/NodeList
for multiple toggles
require(["dojo/query"], function(query){
query(".toggleMe").toggleClass("toggleMe");
});
© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.
http://dojotoolkit.org/api/1.10/dojo/dom-class.html