Returns a new NodeIterator object.
Syntax
var nodeIterator = document.createNodeIterator(root, whatToShow, filter);
Values
root- The root node at which to begin the
NodeIterator's traversal. whatToShowOptional- Is an optional
unsigned longrepresenting a bitmask created by combining the constant properties ofNodeFilter. It is a convenient way of filtering for certain types of node. It defaults to0xFFFFFFFFrepresenting theSHOW_ALLconstant.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 aTreeWalkerwith 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 aTreeWalkerwith 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 aTreeWalkerwith 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. filterOptional- An object implementing the
NodeFilterinterface; itsacceptNode()method will be called for each node in the subtree based at root which is accepted as included by the whatToShow flag to determine whether or not to include it in the list of iterable nodes (a simple callback function may also be used instead). The method should return one ofNodeFilter.FILTER_ACCEPT,NodeFilter.FILTER_REJECT, orNodeFilter.FILTER_SKIP. See the Example.
Note: Prior to Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9), this method accepted an optional fourth parameter (
entityReferenceExpansion) that is not part of the DOM4 specification, and has therefore been removed. This parameter indicated whether or not the children of entity reference nodes were visible to the iterator. Since such nodes were never created in browsers, this paramater had no effect.Example
var nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
function(node) {
return node.nodeName.toLowerCase() === 'p' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
}
);
var pars = [];
var currentNode;
while (currentNode = nodeIterator.nextNode()) {
pars.push(currentNode);
}
Browser Compatibility
Supported in FF 3.5+, Chrome 1+, Opera 9+, Safari 3+, IE9+, Edge
See Also
Specifications
Document Tags and Contributors
Tags:
Contributors to this page:
erikadoyle,
bakotaco,
teoli,
jwhitlock,
tr3w,
MHasan,
kscarfone,
nairakhil13,
Sheppy,
Ms2ger,
Brettz9,
paul.irish,
Nickolay,
NV,
mva.led,
Taken
Last updated by:
erikadoyle,