topical media & game development

talk show tell print

professional-javascript-06-NodeIteratorExample2.htm / htm



  <html>
      <head>
          <title>NodeIterator Example</title>
          <script type="text/javascript">
  
             var iterator = null;
  
             function makeList() {
                 var oDiv = document.getElementById("div1");
                 var oFilter = new Object;
                 oFilter.acceptNode = function (oNode) {
                     return (oNode.tagName == "P") ? 
                             NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT;
                 };
  
                 iterator = document.createNodeIterator(oDiv, NodeFilter.SHOW_ELEMENT, oFilter, false);
                 //For Firefox: iterator = document.createTreeWalker(oDiv, NodeFilter.SHOW_ELEMENT, oFilter, false);
                 var oOutput = document.getElementById("text1");
                 var oNode = iterator.nextNode();
                 while (oNode) {
                     oOutput.value += oNode.tagName + "\n";
                     oNode = iterator.nextNode();
                 }
  
             }
  
          </script>
      </head>
      <body>
          <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>
              <ul>
                  <li>List item 1</li>
                  <li>List item 2</li>
                  <li>List item 3</li>
              </ul>
          </div>
          <textarea rows="10" cols="40" id="text1"></textarea><br />
          <input type="button" value="Make List" onclick="makeList()" />
      </body>
  </html>
  


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