document.createNodeIterator() is an instance method that returns a NodeIterator for walking a DOM subtree (see MDN Document: createNodeIterator()). Learn root, whatToShow, custom filters, nextNode() loops, how it compares to TreeWalker and querySelectorAll, and five try-it labs.
01
Kind
Instance method
02
Returns
NodeIterator
03
Start
root node
04
Filter
whatToShow
05
Walk with
nextNode()
06
Status
Baseline
Fundamentals
Introduction
Sometimes you need every matching node under a branch of the tree — including text nodes or comments that CSS selectors ignore. A NodeIterator walks that subtree in document order.
document.createNodeIterator(root, whatToShow, filter) builds the iterator. Then you call nextNode() until it returns null. MDN’s example collects every <p> under document.body that passes a filter.
💡
Think: filtered tree walk
1) Pick a root 2) Limit types with whatToShow (optional) 3) Accept/skip with a filter (optional) 4) Loop nextNode()
For simple element lists, querySelectorAll is often enough. Use NodeIterator when you need node-type control or text/comment traversal.
After two nextNode calls, the reference node is the second li. previousNode() returns that same node again when stepping back from just past it — experiment in the try-it lab to feel the cursor.
Applications
🚀 Common Use Cases
Collect filtered elements — walk a branch and keep only certain tags (MDN p sample).
Text extraction — iterate SHOW_TEXT under a container.
Comment / PI scans — nodes CSS cannot select.
Custom accept rules — class names, data attributes, or content checks in acceptNode.
Not always needed — prefer querySelectorAll for simple element lists.
Need tree moves? — consider createTreeWalker instead.
🧠 How createNodeIterator() Works
1
Choose a root
MDN: traversal starts at this node’s subtree.
Root
2
Apply whatToShow
Bitmask keeps only selected node types (elements, text, …).
Types
3
Run the filter
Accept or skip each candidate; children still considered when skipped (MDN).
Filter
4
✓
Loop nextNode()
Collect nodes until the iterator returns null.
Important
📝 Notes
MDN: Baseline Widely available since July 2015.
For createNodeIterator, FILTER_REJECT and FILTER_SKIP are equivalent (MDN).
SHOW_ATTRIBUTE is special — prefer Element.attributes for attributes (MDN).
Some older show constants (SHOW_ENTITY, …) are legacy and no longer effective (MDN).
Iterator position matters when mixing nextNode and previousNode.
Document.createNodeIterator() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createNodeIterator()
Create NodeIterator objects for filtered DOM traversal in every major browser.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createNodeIterator()Wide
Bottom line: Use NodeIterator for filtered walks (including text/comments). Prefer querySelectorAll for simple element lists.
Wrap Up
Conclusion
document.createNodeIterator(root, whatToShow, filter) builds a NodeIterator for walking a subtree with optional type and custom filters. Loop with nextNode() until null, and reach for querySelectorAll when you only need simple element matches.
Assume FILTER_REJECT prunes children here (it does not for NodeIterator; MDN)
Rely on SHOW_ATTRIBUTE for normal element trees (MDN)
Forget that text nodes exist between elements
Mutate the tree mid-walk without understanding live DOM effects
Overuse iterators where a selector is clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createNodeIterator()
Filtered DOM walks with nextNode().
5
Core concepts
📝01
Returns
NodeIterator
MDN
🔄02
Walk
nextNode()
loop
📄03
Types
whatToShow
bitmask
⚖️04
Filter
acceptNode
optional
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createNodeIterator() returns a new NodeIterator for walking the DOM subtree starting at a root node. You usually call nextNode() in a loop until it returns null.
No. MDN marks Document.createNodeIterator() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A new NodeIterator object (MDN).
MDN: an optional bitmask from NodeFilter constants (for example NodeFilter.SHOW_ELEMENT or SHOW_TEXT). It defaults to SHOW_ALL (0xFFFFFFFF).
MDN: a callback or an object with acceptNode() that returns FILTER_ACCEPT, FILTER_REJECT, or FILTER_SKIP. For createNodeIterator, FILTER_REJECT and FILTER_SKIP are equivalent — the node is skipped, but children continue to be considered.
Use TreeWalker when you need richer navigation (parentNode, firstChild, nextSibling, and so on). NodeIterator is a simpler forward/back iterator with nextNode() and previousNode().
Did you know?
On a TreeWalker, FILTER_REJECT can prune an entire subtree, but MDN notes that for createNodeIterator, FILTER_REJECT and FILTER_SKIP behave the same: the node is omitted, yet its children can still appear later in the walk.