W3cubDocs

/Dojo

dojo/dom-class

Summary

This module defines the core dojo DOM class API.

See the dojo/dom-class reference documentation for more information.

Methods

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.

Examples

Example 1

Add a class to some node:

require(["dojo/dom-class"], function(domClass){
    domClass.add("someNode", "anewClass");
});

Example 2

Add two classes at once:

require(["dojo/dom-class"], function(domClass){
    domClass.add("someNode", "firstClass secondClass");
});

Example 3

Add two classes at once (using array):

require(["dojo/dom-class"], function(domClass){
    domClass.add("someNode", ["firstClass", "secondClass"]);
});

Example 4

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

Examples

Example 1

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.

Examples

Example 1

Remove a class from some node:

require(["dojo/dom-class"], function(domClass){
    domClass.remove("someNode", "firstClass");
});

Example 2

Remove two classes from some node:

require(["dojo/dom-class"], function(domClass){
    domClass.remove("someNode", "firstClass secondClass");
});

Example 3

Remove two classes from some node (using array):

require(["dojo/dom-class"], function(domClass){
    domClass.remove("someNode", ["firstClass", "secondClass"]);
});

Example 4

Remove all classes from some node:

require(["dojo/dom-class"], function(domClass){
    domClass.remove("someNode");
});

Example 5

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.

Examples

Example 1

require(["dojo/dom-class"], function(domClass){
    domClass.replace("someNode", "add1 add2", "remove1 remove2");
});

Example 2

Replace all classes with addMe

require(["dojo/dom-class"], function(domClass){
    domClass.replace("someNode", "addMe");
});

Example 3

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.

Examples

Example 1

require(["dojo/dom-class"], function(domClass){
    domClass.toggle("someNode", "hovered");
});

Example 2

Forcefully add a class

require(["dojo/dom-class"], function(domClass){
    domClass.toggle("someNode", "hovered", true);
});

Example 3

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