The NodeIterator interface represents an iterator over the members of a list of the nodes in a subtree of the DOM. The nodes will be returned in document order.
A NodeIterator can be created using the Document.createNodeIterator() method, as follows:
var nodeIterator = document.createNodeIterator(root, whatToShow, filter);
Properties
This interface doesn't inherit any property.
NodeIterator.rootRead only- Returns a
Noderepresenting the root node as specified when theNodeIteratorwas created. NodeIterator.whatToShowRead only- Returns an
unsigned longbeing a bitmask made of constants describing the types ofNodethat must to be presented. Non-matching nodes are skipped, but their children may be included, if relevant. The possible values are:Constant Numerical value Description NodeFilter.SHOW_ALL-1(that is the max value ofunsigned long)Shows all nodes. NodeFilter.SHOW_ATTRIBUTE2Shows attribute Attrnodes. This is meaningful only when creating aNodeIteratorwith anAttrnode as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. Since attributes are never children of other nodes, they do not appear when traversing over the document tree.NodeFilter.SHOW_CDATA_SECTION8Shows CDATASectionnodes.NodeFilter.SHOW_COMMENT128Shows Commentnodes.NodeFilter.SHOW_DOCUMENT256Shows Documentnodes.NodeFilter.SHOW_DOCUMENT_FRAGMENT1024Shows DocumentFragmentnodes.NodeFilter.SHOW_DOCUMENT_TYPE512Shows DocumentTypenodes.NodeFilter.SHOW_ELEMENT1Shows Elementnodes.NodeFilter.SHOW_ENTITY32Shows Entitynodes. This is meaningful only when creating aNodeIteratorwith anEntitynode as its root; in this case, it means that theEntitynode will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.NodeFilter.SHOW_ENTITY_REFERENCE16Shows EntityReferencenodes.NodeFilter.SHOW_NOTATION2048Shows Notationnodes. This is meaningful only when creating aNodeIteratorwith aNotationnode as its root; in this case, it means that theNotationnode will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.NodeFilter.SHOW_PROCESSING_INSTRUCTION64Shows ProcessingInstructionnodes.NodeFilter.SHOW_TEXT4Shows Textnodes. NodeIterator.filterRead only- Returns a
NodeFilterused to select the relevant nodes. NodeIterator.expandEntityReferencesRead only- Is a
Booleanindicating if, when discarding anEntityReferenceits whole sub-tree must be discarded at the same time. NodeIterator.referenceNodeRead only- Returns the
Nodeto which the iterator is anchored. NodeIterator.pointerBeforeReferenceNodeRead only- Returns a
Booleanflag that indicates whether theNodeIteratoris anchored before, the flag beingtrue, or after, the flag beingfalse, the anchor node.
Methods
This interface doesn't inherit any method.
NodeIterator.detach()- This operation is a no-op. It doesn't do anything. Previously it was telling the engine that the
NodeIteratorwas no more used, but this is now useless. NodeIterator.previousNode()- Returns the previous
Nodein the document, ornullif there are none. NodeIterator.nextNode()- Returns the next
Nodein the document, ornullif there are none.
Specifications
| Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'NodeIterator' in that specification. |
Living Standard | Added the referenceNode and pointerBeforeReferenceNode properties.Removed the expandEntityReferences property.The method detach() has been changed to be a no-op.The methods previousNode() and nextNode() don't raise an exception any more. |
| Document Object Model (DOM) Level 2 Traversal and Range Specification The definition of 'NodeIterator' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 1.0 | 3.5 (1.9.1) | 9.0 | 9.0 | 3.0 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes) | 1.0 (1.9.1) | (Yes) | 9.0 | 3.0 |
See also
- The creator method:
Document.createNodeIterator(). - Related interfaces:
NodeFilter,TreeWalker.