dojo.hasClass

Project owner:Eugene Lazutkin
Available:since V0.9

Returns a boolean depending on whether or not a node has a passed class string.

Usage

The function takes two arguments:

  1. A DOM node or its node id (as a string).
  2. A CSS class name as a string.

It returns true if the node has the class, and false otherwise.

Consider the following html:

1
2
  <div id="bam" class="foo bar baz"></div>
  <div class="something else"></div>

Using dojo.hasClass to find if the node id="bam" has class="foo":

1
2
3
if(dojo.hasClass("bam", "foo")){
  /* it does */
}

Using dojo.query to find a node and check if it has a class:

1
2
3
4
5
dojo.query(".something").forEach(function(node){
   if(dojo.hasClass(node, "else"){
      /* it does */
   }
});

Note: You do not need to explicitly check for dojo.hasClass before adding or removing a class with dojo.addClass or dojo.removeClass, they do it for you.

Table of Contents

Error in the documentation? Can’t find what you are looking for? Let us know!