This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

createNodeIterator

Summary

Creates a NodeIterator object that you can use to traverse filtered lists of nodes or elements in a document.

Method of dom/Documentdom/Document

Syntax

var nodeIterator = document.createNodeIterator(rootNode, whatToShow, filter, expandEntityReference);

Parameters

rootNode

Data-type
DOM Node

The root element or node to start traversing on.

whatToShow

Data-type
unsigned long

The type of nodes or elements to appear in the node list. For more information, see whatToShow.

filter

Data-type
DOM Node

A custom NodeFilter function to use. For more information, see filter. Use null for no filter.

expandEntityReference

Data-type
Boolean

Whether entity reference nodes are expanded. For more information, see expandEntityReferences.

Return Value

Returns an object of type DOM NodeDOM Node

A NodeIterator instance object.

Examples

The following code example shows how to use NodeIterator objects to find and remove references. The iterator returns all text nodes from the document body and searches for Monday in text and id attributes of parent nodes. The script matches text by using the wholeText object of the node.

<!DOCTYPE html>
<html>
 <head>
  <script>
function noMondays()
{
    var ni = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT, null, false);

    var textNode = ni.nextNode();
    while (textNode) {
        if (textNode.wholeText.match('Monday') ||
            textNode.parentNode.getAttribute('id') == 'Monday')
        {
            textNode.parentNode.removeChild(textNode);
        }
        textNode = ni.nextNode();
    }
}
function refresh()
{
    window.location.reload( false );    // Reload our page.
}
  </script>
 </head>
 <body>
    <p>Monday, Joe bought a turkey.</p>
    <p>Tuesday, Bill bought a pound of nails.</p>
    <div>Chuck called in sick Monday.</div>
    <p id="Monday">CALL supplier today!</p>
    <p>Wednesday's delivery was delayed.</p>
    <button onclick="refresh()">Reload</button>
    <button onclick="noMondays()">Lose Mondays</button>
 </body>
</html>

Notes

Use the createNodeIterator method when you want to focus on node content because NodeIterator traverses a flat list of nodes in the document structure.

Related specifications

DOM Level 2 Traversal and Range
Recommendation

Attributions