topical media & game development

talk show tell print

professional-javascript-06-NodeIteratorExample1.htm / htm

          <title>NodeIterator Example</title>
          <script type="text/javascript">
             var iterator = null;
             function makeList() {
                 var oDiv = document.getElementById("div1");
                 iterator = document.createNodeIterator(oDiv, NodeFilter.SHOW_ELEMENT, null, false);
                 //For Firefox: iterator = document.createTreeWalker(oDiv, NodeFilter.SHOW_ELEMENT, null, false);
                 var oOutput = document.getElementById("text1");
                 var oNode = iterator.nextNode();
                 while (oNode) {
                     oOutput.value += oNode.tagName + "\n";
                     oNode = iterator.nextNode();
          <p><strong>Note:</strong> The <code>NodeIterator</code> object has only been implemented in Opera (version 7.6 and higher) and Safari (version 1.3 and higher). It has not been implemented in Internet Explorer or Firefox (so this example won't work). In Firefox, you can use the <code>TreeWalker</code> instead.</p>
          <div id="div1">
              <p>Hello <b>World!</b></p>
                  <li>List item 1</li>
                  <li>List item 2</li>
                  <li>List item 3</li>
          <textarea rows="10" cols="40" id="text1"></textarea><br />
          <input type="button" value="Make List" onclick="makeList()" />

(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.