Login Register

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:

<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>

This example doesn't work

This example doesn't work for my Firefox 2.0.0.11, it only shows the first button and outputs on console '1' and '0', but works on IE7.

This version worked on my Firefox's version.

<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>
   <button id="b2" ></button>
   <form id="thisForm" >
      <button id="formB" ></button>
   </form>
</body>