filter
Summary
Gets the currently applied NodeFilter to the traversal.
Property of dom/NodeIteratordom/NodeIterator
Syntax
Note: This property is read-only.
var nodeFilter = nodeIterator.filter;
Return Value
Returns an object of type DOM NodeDOM Node
The NodeFilter that was applied while traversing.
Examples
The following example searches for table and anchor tags and reports the value of the id attribute. Although the TreeWalker preserves the hierarchical relationship of nodes, you don’t need to write recursive functions to walk the nodes in a hierarchy. The NodeFilter function skips nodes rather than rejecting them, which allows the function to examine all child nodes in the hierarchy.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
// This is the NodeFilter function. It receives a node, and must return a NodeFilter flag.
function filter(node)
{
if (node.tagName == "TABLE" || node.tagName == "A")
return NodeFilter.FILTER_ACCEPT;
return NodeFilter.FILTER_SKIP;
}
function findNodes()
{
var tw = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT, filter, false);
var node, results = '';
while (node = tw.nextNode()) {
results += node.id + "<br/>";
}
document.getElementById("results").innerHTML += results;
}
function refresh()
{
window.location.reload( false ); // Reload our page.
}
</script>
</head>
<body>
<!-- this is a comment node-->
<table border="1" id="myTable">
<tbody>
<tr/>
<tr>
<td><a href="" id="myLink">Text inside anchor</a></td>
</tr>
</tbody>
</table>
<div id="results">Results:<br/></div>
<button onclick="findNodes()">Find Nodes</button>
<button onclick="refresh()">Reload</button>
</body>
</html>
Usage
Use the filter property to exclude/include Nodes from the Iteration.
Notes
- Appending content to the document while the TreeWalker is searching for nodes can cause an endless loop. To prevent this, the example collects all possible output in a temporary variable and appends it to the document after the TreeWalker is finished.
- The NodeFilter is a callback function that provides customized filtering for NodeIterator and TreeWalker. The filter function accepts a node as its only parameter, and indicates whether the node is accepted, rejected, or skipped.
function myFilter(node) {
// NodeFilter function that returns one of the following flags:
// NodeFilter.FILTER_ACCEPT, NodeFilter.FILTER_REJECT, NodeFilter.FILTER_SKIP
}
Related specifications
- DOM Level 2 Traversal and Range
- Recommendation
Attributions
Mozilla Developer Network : [NodeIterator.filter Article]
Microsoft Developer Network: [filter Property Article]