The filter property of a NodeIterator returns the NodeFilter object used to screen nodes during traversal. Learn how acceptNode() chooses FILTER_ACCEPT or FILTER_REJECT, how it relates to document.createNodeIterator(), and how to walk nodes with nextNode()—with five examples and try-it labs.
01
Kind
Instance property
02
Returns
NodeFilter
03
Method
acceptNode()
04
Writable?
Read-only
05
Set at
createNodeIterator
06
Status
Baseline widely
Fundamentals
Introduction
A NodeIterator walks a DOM subtree in document order. When you create one with document.createNodeIterator(root, whatToShow, filter), the third argument is a filter object. The iterator stores that object on its read-only filter property.
The filter’s acceptNode(node) method runs on candidate nodes. Return NodeFilter.FILTER_ACCEPT to include a node in the walk, or NodeFilter.FILTER_REJECT to skip it.
💡
Beginner tip
whatToShow limits node types (elements, text, etc.). filter adds custom rules on top—like “only <p> tags” or “skip nodes with class hidden.”
Concept
Understanding the filter Property
A read-only instance property on NodeIterator that exposes the NodeFilter passed when the iterator was created.
Returns — a NodeFilter object with acceptNode(node).
Read-only — you cannot assign a new filter after creation.
acceptNode — called for nodes that pass the whatToShow bitmask.
FILTER_ACCEPT — node is included when you call nextNode().
FILTER_REJECT — node is skipped (on NodeIterator, same as FILTER_SKIP).
Baseline Widely available on MDN (since July 2015).
Foundation
📝 Syntax
Read the property on a NodeIterator:
JavaScript
nodeIterator.filter
Return value
A NodeFilter object (the one passed to createNodeIterator).
Typical pattern (MDN idea)
JavaScript
const nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
{
acceptNode(node) {
return NodeFilter.FILTER_ACCEPT;
},
},
);
const nodeFilter = nodeIterator.filter;
// same object passed as the third argument
NodeIterator.filter is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
NodeIterator.filter
Read-only NodeFilter with acceptNode() — set when you call document.createNodeIterator().
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported on NodeIterator (legacy DOM)
Legacy OK
NodeIterator.filterExcellent
Bottom line: Pass your filter at createNodeIterator time — iterator.filter exposes it for reading and manual acceptNode tests.
Wrap Up
Conclusion
NodeIterator.filter exposes the NodeFilter you passed to document.createNodeIterator(). Its acceptNode(node) method decides which nodes appear when you call nextNode().
Define filter logic when calling createNodeIterator
Return FILTER_ACCEPT or FILTER_REJECT explicitly
Combine whatToShow with a custom filter for precision
Use iterator.filter.acceptNode(node) to debug one node
Prefer querySelectorAll when CSS alone is enough
❌ Don’t
Try to assign a new value to iterator.filter
Forget that the filter runs after whatToShow pre-screening
Assume FILTER_SKIP behaves differently from FILTER_REJECT on NodeIterator
Use NodeIterator when a simple array from querySelectorAll suffices
Mutate the DOM heavily mid-walk without understanding iterator state
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about NodeIterator.filter
The NodeFilter that screens your walk.
5
Core concepts
🔗01
Returns
NodeFilter
API
⚙️02
Read-only
no assign
Rule
🔒03
acceptNode
per node
Method
📄04
ACCEPT
vs REJECT
Compare
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
A read-only NodeFilter object—the same filter passed as the third argument to document.createNodeIterator(). It has an acceptNode(node) method used to screen nodes during traversal.
No. MDN marks NodeIterator.filter as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. filter is read-only. Create a new NodeIterator with a different filter if you need different screening rules.
Return NodeFilter.FILTER_ACCEPT to include a node in the walk, or NodeFilter.FILTER_REJECT to skip it. On NodeIterator, FILTER_REJECT and FILTER_SKIP behave the same.
whatToShow is a bitmask of node types (elements, text, etc.). filter is a custom acceptNode function for finer control—e.g. only paragraphs with a certain class.
Use querySelectorAll for simple CSS matches. Use createNodeIterator with a filter when you need document-order walks with custom accept/reject logic on nodes.
Did you know?
MDN’s sample reads nodeIterator.filter after creation—the same object you passed in. You can also call filter.acceptNode(someNode) yourself to test accept/reject without walking the whole tree.