TreeWalker.filter

  • Revision slug: Web/API/treeWalker.filter
  • Revision title: treeWalker.filter
  • Revision id: 405513
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Moved From DOM/treeWalker.filter to Web/API/treeWalker.filter

Revision Content

{{ DomRef() }}

Summary

Returns an object with a method acceptNode(node).

Syntax

nodeFilter = treeWalker.filter;

Example

var treeWalker = document.createTreeWalker(
    document.body,
    NodeFilter.SHOW_ELEMENT,
    { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
    false
);
nodeFilter = treeWalker.filter; // document.body in this case

Notes

When creating the TreeWalker, the filter object is passed in as the third parameter, and the object method acceptNode(node) is called on every single node to determine whether or not to accept it. This function should return the constant NodeFilter.FILTER_ACCEPT for cases when the node should be accepted and NodeFilter.FILTER_REJECT for cases when the node should be rejected.

Revision Source

<p>
{{ DomRef() }}
</p>
<h3 id="Summary" name="Summary"> Summary </h3>
<p>Returns an object with a method <code>acceptNode(node)</code>.
</p>
<h3 id="Syntax" name="Syntax"> Syntax </h3>
<pre class="eval">nodeFilter = treeWalker.filter;
</pre>
<h3 id="Example" name="Example"> Example </h3>
<pre class="eval">var treeWalker = document.createTreeWalker(
    document.body,
    NodeFilter.SHOW_ELEMENT,
    { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
    false
);
nodeFilter = treeWalker.filter; // document.body in this case
</pre>
<h3 id="Notes" name="Notes"> Notes </h3>
<p>When creating the TreeWalker, the filter object is passed in as the third parameter, and the object method <code>acceptNode(node)</code> is called on every single node to determine whether or not to accept it. This function should return the constant <code>NodeFilter.FILTER_ACCEPT</code> for cases when the node should be accepted and <code>NodeFilter.FILTER_REJECT</code> for cases when the node should be rejected.
</p>
Revert to this revision