Selecting DOM Nodes with dojo.query

XHR is half of the Ajax story. Once you make a request for data and receive it via XHR, you must change the page - display the new data in a panel, turn an indicator from red to green, or whatever . Changing HTML is, in turn, dependent on locating nodes.

To select HTML elements in JavaScript, you can use the browser's native DOM API, but they're verbose and hard to work with...not to mention slow. For example, retrieving all nodes with the class "progressIndicator" uses this code:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
var list = [];
var nodes = document.getElementsByTagName("*");
// iterate over every node in the document....SLOOOW
for(var x = 0; x < nodes.length; x++){
  if(nodes[x].className == "progressIndicator"){
     list.push(nodes[x]);
  }
}
console.dir(list);

Oy! That's a lot of code for what should be very simple. It's also very slow. dojo.query gives us a more compact way to do it, and it's often faster, particularly as we ask for more sophisticated kinds of relationships. The following is exactly equivalent to our first example:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
console.dir( dojo.query(".progressIndicator") );

The use of CSS means you don't have to learn a whole new query language or try to manaully write out what we mean in DOM code. To top it all off, it's fast too - see the original article on dojo.query for details.

Here are some other types of queries that dojo.query will support

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
// portably supports most CSS 3 selectors
dojo.query(".someClassName");
dojo.query("div.someClassName");
dojo.query("tbody tr:nth-child(odd)");
dojo.query("[dojoType~=dijit]");
dojo.query("[name^=item]");
dojo.query(".someClass", "scopeId"); // scoped searches
// returns a REAL array
dojo.query(".someClassName").length;
dojo.query(".someClassName")[3];
dojo.query(".someClassName").concat(dojo.query("span"));

Read on to find out more!

Selecting Nodes by Tag, ID or Class

The most popular methods for selecting nodes are by tag name, or the class and id attributes of the tag. Those are the most popular selectors for CSS files too. So, as you'd expect, dojo.query makes these easy:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
// Query by tag.  Equivalent to DOM document.getElementsByTagName("IMG");
console.dir(dojo.query("img"));   // Non-case sensitive, so IMG would work too.
// Query by class.  Equivalent to big loop on previous page
console.dir(dojo.query(".offToSeeTheWij"));   
// Query by id.  Equivalent to DOM document.getElementById("widget123");
// or dojo.byId("widget123")
console.dir(dojo.query("#widget123"));

As with CSS, you can combine selectors in dojo.query to create Compound Selectors. By smooshing the selectors "a" and "b" against each other, you say "select nodes that have both properties a and b", as in: /* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}

// Select just image tags with the class offToSeeTheWij
console.dir(dojo.query("img.offToSeeTheWij"));

Things to Make and Do With Queries

dojo.query, like CSS3, can do very complex selecting. But first ... what can you do with the retrieved nodes?

dojo.query returns a standard DOM NodeList, which can be traversed with regular JavaScript array techniques. It has a .length property like any array, and you can loop through it with a for loop. But Dojo provides some more convenient methods:

Applying a Dojo Function to Each Element

Since dojo.query returns a NodeList object, you can feed the results into any NodeList method. Some popular Dojo methods can be tacked right onto a dojo.query. Take for example style() which applies a style to each element queried. The following turns the background color of each element in class disableAble to gray:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
dojo.query(".disableAble").style("backgroundColor","gray");

Here is a complete list of Dojo methods that can be used in this manner.

  • indexOf()
  • lastIndexOf()
  • coords()
  • style()
  • styles()
  • addClass()
  • removeClass()
  • place()
  • connect()
  • orphan()
  • adopt()
  • addContent()

For Dojo 1.0: if you do a dojo.require("dojo.NodeList-fx"), the following methods will be added to the NodeList object (see API documentation for more information):

  • wipeIn()
  • wipeOut()
  • slideTo()
  • fadeIn()
  • fadeOut()
  • animateProperty()

More General Functions - forEach

New for 1.0forEach() applies a snippet of JavaScript to each node, as described in Functions Used Everywhere. This snippet can use the following "magic" variables:

  • item is the DOM node currently being examined
  • index is its position in the NodeList, starting from 0
  • arr is the entire array of nodes. You can use this to perform lookahead or look-behind. arr[index] === item is always true.

The following code disables all INPUT tags

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
dojo.query("input").forEach("item.disabled = true;");

This call style is supported by forEach(), map(), every(), and some(). dojo.filter() also supports it but the NodeList filter() method does not since it accepts a CSS string expression to filter by instead.

The above only works for Dojo 1.0, but in either 0.9 or 1.0 you can pass a function name or a function literal. The function must take at least one parameter, the first of which is the element being examined. (The rest of the parameters are ignored). So the following code is equivalent to the first example:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
dojo.query("input").forEach(
    function(inputElement) {
        inputElement.disabled = true;
   }
);

Filter

filter() uses a JavaScript function to pare down a list of nodes. The function provided to filter must take a node as input (similar to forEach) and return true if the node is to be kept. It returns a nodeList, just like query. This is especially useful for element selections you cannot express in CSS selector language. For example, odd/even row coloring can be performed this way:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
var nodeCount = 0;
dojo.query("tr").filter(
   function(thisRow) {
      return (nodeCount++) % 2 == 0;
   }
).style("backgroundColor",evenRowColor);

Note that, as we said before, NodelList filter cannot use the new 1.0 snippet syntax. You must pass in a function literal or function name.

Event Handlers

You can attach event handlers to all the nodes of a query. For example, to bind an onclick handler to all elements with the "deadLink" class:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
dojo.query(".deadLink").onclick(function(evt){
    dojo.stopEvent(evt);
});

NodeList methods are chainable:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #000066; font-weight: bold;} .geshifilter .kw2 {color: #003366; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .co1 {color: #009900; font-style: italic;} .geshifilter .coMULTI {color: #009900; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #3366CC;} .geshifilter .nu0 {color: #CC0000;} .geshifilter .me1 {color: #006600;} .geshifilter .re0 {color: #0066FF;}
dojo.query(".deadLink").addClass("disabledLink").onclick(function(evt){
    dojo.stopEvent(evt);
});

The following are event handler binders available on a NodeList object (the object returned from a dojo.query() call -- see dojo.NodeList API documentation for more information):

  • onblur()
  • onclick()
  • onkeydown()
  • onkeypress()
  • onkeyup()
  • onmousedown()
  • onmouseenter()
  • onmouseleave()
  • onmousemove()
  • onmouseout()
  • onmouseover()
  • onmouseup()
'

Relative Position Query

An optional second parameter to dojo.query indicates the root node for searching. If a string is passed, dojo.query() assumes it to be the ID of the element to use as the search root, otherwise it will expect a DOM node. In all of our examples so far, the second parameter has been left out, and defaults to document, the root of the HTML page. (Actually, it's dojo.doc, which is "document", but can be modified with dojo.withDocument).

The following shows how the second parameter affects the result:

/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */ .geshifilter {font-family: monospace;} .geshifilter .imp {font-weight: bold; color: red;} .geshifilter .kw1 {color: #b1b100;} .geshifilter .kw2 {color: #000000; font-weight: bold;} .geshifilter .kw3 {color: #000066;} .geshifilter .coMULTI {color: #808080; font-style: italic;} .geshifilter .es0 {color: #000099; font-weight: bold;} .geshifilter .br0 {color: #66cc66;} .geshifilter .st0 {color: #ff0000;} .geshifilter .nu0 {color: #cc66cc;} .geshifilter .sc0 {color: #00bbdd;} .geshifilter .sc1 {color: #ddbb00;} .geshifilter .sc2 {color: #009900;}
<html>
<head>
 <script type="text/javascript" src="../js/dojo/dojo.js"></script>
 <script type="text/javascript">
     dojo.addOnLoad(function() {
         console.debug(dojo.query("button").length);  // Outputs "3"
         console.debug(dojo.query("button", "thisForm").length);  // outputs 1
    });
</script>
</head>
<body>
   <button id="b1" />
   <button id="b2" />
   <form id="thisForm" >
      <button id="formB" />
   </form>
</body>
</html>

Standard CSS3 Selectors

Because dojo.query adopts the CSS3 standard for selecting nodes, you can use any CSS reference guide for help on choosing the right queries. Eric Meyer's CSS: The Definitive Guide is a good resource. For convenience, here's a chart of the standard CSS3 selectors, taken from the current working draft RFC.

Pattern Meaning
* any element
E an element of type E
E[foo] an E element with a "foo" attribute
E[foo="bar"] an E element whose "foo" attribute value is exactly equal to "bar"
E[foo~="bar"] an E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "bar"
E[foo^="bar"] an E element whose "foo" attribute value begins exactly with the string "bar"
E[foo$="bar"] an E element whose "foo" attribute value ends exactly with the string "bar"
E[foo*="bar"] an E element whose "foo" attribute value contains the substring "bar"
E[hreflang|="en"] an E element whose "hreflang" attribute has a hyphen-separated list of values beginning (from the left) with "en"
E:root an E element, root of the document
E:nth-child(n) an E element, the n-th child of its parent
E:nth-last-child(n) an E element, the n-th child of its parent, counting from the last one
E:nth-of-type(n) an E element, the n-th sibling of its type
E:nth-last-of-type(n) an E element, the n-th sibling of its type, counting from the last one
E:first-child an E element, first child of its parent
E:last-child an E element, last child of its parent
E:first-of-type an E element, first sibling of its type
E:last-of-type an E element, last sibling of its type
E:only-child an E element, only child of its parent
E:only-of-type an E element, only sibling of its type
E:empty an E element that has no children (including text nodes)
E:link
E:visited
an E element being the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited)
E:active
E:hover
E:focus
an E element during certain user actions
E:target an E element being the target of the referring URI
E:lang(fr) an element of type E in language "fr" (the document language specifies how language is determined)
E:enabled
E:disabled
a user interface element E which is enabled or disabled
E:checked a user interface element E which is checked (for instance a radio-button or checkbox)
E::first-line the first formatted line of an E element
E::first-letter the first formatted letter of an E element
E::selection the portion of an E element that is currently selected/highlighted by the user
E::before generated content before an E element
E::after generated content after an E element
E.warning an E element whose class is "warning" (the document language specifies how class is determined).
E#myid an E element with ID equal to "myid".
E:not(s) an E element that does not match simple selector s
E F an F element descendant of an E element
E > F an F element child of an E element
E + F an F element immediately preceded by an E element
E ~ F an F element preceded by an E element