Document.createTreeWalker()

  • Revision slug: DOM/document.createTreeWalker
  • Revision title: document.createTreeWalker
  • Revision id: 30480
  • Created:
  • Creator: matiasp
  • Is current revision? No
  • Comment no wording changes; page display name changed to 'document.createTreeWalker'

Revision Content

{{ DomRef() }}

Summary

Returns a new TreeWalker object.

Syntax

var treeWalker = document.createTreeWalker(root, whatToShow, filter, entityReferenceExpansion);
  • root is the root of the TreeWalker's traversal
  • whatToShow is one of the constant properties of NodeFilter
  • filter is an object with a method acceptNode, which is called by the TreeWalker to determine whether or not to accept a node. The function should return NodeFilter.FILTER_ACCEPT, NodeFilter.FILTER_REJECT, or NodeFilter.FILTER_SKIP. (See example)
  • entityReferenceExpansion

Example

var treeWalker = document.createTreeWalker(
    document.body,
    NodeFilter.SHOW_ELEMENT,
    { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
    false
);
var nodeList = new Array();
while(treeWalker.nextNode()) nodeList.push(treeWalker.currentNode);

Specification

DOM Level 2 Traversal: TreeWalker

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Returns a new <code><a href="/en/DOM/treeWalker" title="en/DOM/treeWalker">TreeWalker</a></code> object.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">var treeWalker = document.createTreeWalker(root, whatToShow, filter, entityReferenceExpansion);
</pre>
<ul> <li><code>root</code> is the root of the TreeWalker's traversal</li> <li><code>whatToShow</code> is one of the constant properties of <code><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html#Traversal-NodeFilter" title="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html#Traversal-NodeFilter">NodeFilter</a></code></li> <li><code>filter</code> is an object with a method <code>acceptNode</code>, which is called by the TreeWalker to determine whether or not to accept a node. The function should return <code>NodeFilter.FILTER_ACCEPT</code>, <code>NodeFilter.FILTER_REJECT</code>, or <code>NodeFilter.FILTER_SKIP</code>. (See example)</li> <li><code>entityReferenceExpansion</code></li>
</ul>
<h3 name="Example">Example</h3>
<pre class="eval">var treeWalker = document.createTreeWalker(
    document.body,
    NodeFilter.SHOW_ELEMENT,
    { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
    false
);
var nodeList = new Array();
while(treeWalker.nextNode()) nodeList.push(treeWalker.currentNode);
</pre>
<h3 name="Specification">Specification</h3>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html#Traversal-TreeWalker">DOM Level 2 Traversal: TreeWalker</a></p>
Revert to this revision