You are here: Reference > JavaScript > client-side > HTML DOM > methods > createNodeIterator (document, XMLDocument)
createNodeIterator method (document, XMLDocument)
9 | 3.5 | |||
Creates a NodeIterator object that can be used to iterate through the nodes in a subtree.
Note: The createNodeIterator method is supported and implemented in Firefox from version 3.5.
In the earlier versions of Firefox, it is supported but not implemented, using it raises an exception.
The NodeIterator object represents the result of the createNodeIterator method as an ordered list. The members of the NodeIterator object can be used to navigate through the result list. For a detailed description, please see the page for the NodeIterator object.
The createTreeWalker method and the TreeWalker object are similar to the createNodeIterator method and the NodeIterator object. The main difference is that the TreeWalker object represents the result as a tree not as an ordered list.Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Reference to a node that will be the root node of the NodeIterator object. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Integer that specifies the type of nodes to show.
Predefined constants are available for the possible values of this parameter, in the scope of the NodeFilter interface (e.g. NodeFilter.SHOW_ALL).
The value can be any combination of the following integer constants with the bitwise OR operator (the value of a predefined constant appears in parentheses after the constant in hexadecimal form):
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Reference to a user-specified function that can filter nodes.
The method gets a node for checking and must return an integer value as a result.
Predefined constants are available for the possible results, in the scope of the NodeFilter interface (e.g. NodeFilter.FILTER_ACCEPT).
The result can be one of the following values (the value of a predefined constant appears in parentheses after the constant):
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Boolean that indicates whether the contents of EntityReference nodes are visible for the NodeIterator object.
One of the following values:
|
Return value:
Returns the newly created NodeIterator object.
Example HTML code 1:
This example illustrates the use of the createNodeIterator method:
|
||||
<head> <script type="text/javascript"> function ElementChecker (node) { if (node.tagName.toLowerCase () == 'button') { return NodeFilter.FILTER_ACCEPT; } return NodeFilter.FILTER_SKIP; } function FindFirstButton () { // try..catch is necessary because the createNodeIterator method is supported but not implemented in Firefox before version 3.5. try { iterator = document.createNodeIterator (document, NodeFilter.SHOW_ELEMENT, ElementChecker, false); // get the first matching node var button = iterator.nextNode (); alert ("The label of the first button element:\n" + button.textContent); } catch (e) { alert ("Your browser does not support the createNodeIterator method!"); } } </script> </head> <body> <button onclick="FindFirstButton ()">Find the first button tag with the NodeIterator object</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to iterate through span elements with the NodeIterator object:
|
||||
<head> <script type="text/javascript"> function ElementChecker (node) { if (node.tagName.toLowerCase () == 'span') { return NodeFilter.FILTER_ACCEPT; } return NodeFilter.FILTER_SKIP; } function FindMainSections () { var contElem = document.getElementById ("content"); // try..catch is necessary because the createNodeIterator method is supported but not implemented in Firefox before version 3.5. try { iterator = document.createNodeIterator (contElem, NodeFilter.SHOW_ELEMENT, ElementChecker, false); // get the first matching node var node = iterator.nextNode (); while (node) { alert ("The contents of the section:\n " + node.innerHTML); node = iterator.nextNode (); } } catch (e) { alert ("Your browser does not support the createNodeIterator method!"); } } </script> </head> <body> <div id="content"> <span> <b>1. Section</b><br /> <span> <b>1.1. Subsection</b><br /> </span> </span> <span> <b>2.Section</b><br /> </span> </div> <br /><br /> <button onclick="FindMainSections ()">Find the sections with the NodeIterator object</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments