JavaScript NodeIterator filter Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

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

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.”

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).

📝 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

⚡ Quick Reference

GoalCode / note
Read filteriterator.filter
Accept nodereturn NodeFilter.FILTER_ACCEPT
Reject nodereturn NodeFilter.FILTER_REJECT
Test manuallyiterator.filter.acceptNode(someNode)
Writable?No (read-only)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NodeIterator.filter.

Returns
NodeFilter

Screening object

Writable?
no

Read-only

Key method
acceptNode

Per-node test

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NodeIterator.filter and Document.createNodeIterator().

📚 Getting Started

Read filter and accept every element (MDN shape).

Example 1 — Read filter After Creation (MDN Idea)

Store the filter object and read it back from the iterator.

JavaScript
const nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  {
    acceptNode(node) {
      return NodeFilter.FILTER_ACCEPT;
    },
  },
);

const nodeFilter = nodeIterator.filter;
console.log(typeof nodeFilter.acceptNode); // "function"
Try It Yourself

How It Works

iterator.filter is the same object you passed as the third argument.

Example 2 — Filter to <p> Elements Only

MDN-style: accept paragraphs, reject everything else.

JavaScript
const iterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  {
    acceptNode(node) {
      return node.nodeName.toLowerCase() === "p"
        ? NodeFilter.FILTER_ACCEPT
        : NodeFilter.FILTER_REJECT;
    },
  },
);

const pars = [];
let node;
while ((node = iterator.nextNode())) {
  pars.push(node.textContent);
}
Try It Yourself

How It Works

acceptNode runs on each element candidate; only matching nodes appear in nextNode().

📈 Custom Rules & Walks

Class skips, manual acceptNode, and list walks.

Example 3 — Skip Nodes With a Class

Reject elements that carry class="skip".

JavaScript
const iterator = document.createNodeIterator(
  document.getElementById("box"),
  NodeFilter.SHOW_ELEMENT,
  {
    acceptNode(node) {
      return node.classList.contains("skip")
        ? NodeFilter.FILTER_REJECT
        : NodeFilter.FILTER_ACCEPT;
    },
  },
);
Try It Yourself

How It Works

The filter object on iterator.filter holds this acceptNode logic.

Example 4 — Call filter.acceptNode() Directly

Test the filter on a single node without walking the whole tree.

JavaScript
const iterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

const target = document.querySelector("h1");
const result = iterator.filter.acceptNode(target);
console.log(result === NodeFilter.FILTER_ACCEPT); // true
Try It Yourself

How It Works

Useful for debugging what your filter would do to one node.

Example 5 — Walk Accepted <li> Nodes

Combine SHOW_ELEMENT, a list filter, and nextNode().

JavaScript
const root = document.getElementById("fruit");
const iterator = document.createNodeIterator(
  root,
  NodeFilter.SHOW_ELEMENT,
  {
    acceptNode(node) {
      return node.nodeName === "LI"
        ? NodeFilter.FILTER_ACCEPT
        : NodeFilter.FILTER_REJECT;
    },
  },
);

const labels = [];
let item;
while ((item = iterator.nextNode())) {
  labels.push(item.textContent);
}
Try It Yourself

How It Works

Only <li> elements pass iterator.filter.acceptNode during the walk.

🚀 Common Use Cases

  • Inspect the filter object after creating a NodeIterator.
  • Accept only certain tag names or roles during document-order walks.
  • Skip hidden or disabled nodes with custom acceptNode logic.
  • Debug filtering by calling iterator.filter.acceptNode(node) on one node.
  • Pair with nextNode() to collect nodes that pass both whatToShow and filter.

🔧 How It Works

1

createNodeIterator

Pass root, whatToShow, and a filter object with acceptNode(node).

Create
2

Store on filter

The iterator keeps that NodeFilter on its read-only filter property.

Property
3

Screen nodes

During nextNode(), acceptNode returns FILTER_ACCEPT or FILTER_REJECT.

Filter
4

Walk results

Only accepted nodes are returned; rejected nodes are skipped in the iteration.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • filter is read-only; define rules when calling createNodeIterator.
  • On NodeIterator, FILTER_REJECT and FILTER_SKIP are equivalent.
  • The filter can be a function or an object with acceptNode (MDN accepts both for the third parameter).
  • Related learning: NodeList.forEach(), childNodes, NodeList.length.

Universal Browser Support

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().

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NodeIterator (legacy DOM)
Legacy OK
NodeIterator.filter Excellent

Bottom line: Pass your filter at createNodeIterator time — iterator.filter exposes it for reading and manual acceptNode tests.

Conclusion

NodeIterator.filter exposes the NodeFilter you passed to document.createNodeIterator(). Its acceptNode(node) method decides which nodes appear when you call nextNode().

Continue with pointerBeforeReferenceNode, NodeList.entries(), childNodes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeIterator.filter

The NodeFilter that screens your walk.

5
Core concepts
⚙️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.

Explore pointerBeforeReferenceNode

Next up: read whether the iterator pointer is before or after the anchor.

pointerBeforeReferenceNode →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful