topical media & game development

talk show tell print

professional-javascript-06-TreeWalkerExample1.htm / htm



  <html>
      <head>
          <title>TreeWalker Example</title>
          <script type="text/javascript">
  
             var walker = 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;
                 };
  
                 walker = document.createTreeWalker(oDiv, NodeFilter.SHOW_ELEMENT, oFilter, false);
  
                 var oOutput = document.getElementById("text1");
                 var oNode = walker.nextNode();
                 while (oNode) {
                     oOutput.value += oNode.tagName + "\n";
                     oNode = walker.nextNode();
                 }
  
             }
  
          </script>
      </head>
      <body>
          <p><strong>Note:</strong> The <code>TreeWalker</code> object has only been implemented in Opera (version 7.6 and higher), Safari (version 1.3 and higher), and Firefox (version 1.0 and higher). It has not been implemented in Internet Explorer (so this example won't work).</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.