document.createTreeWalker() returns a TreeWalker rooted at a node. Use whatToShow and an optional filter to pick which nodes are “visible”, then move with nextNode(), firstChild(), and friends (MDN).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.createTreeWalker. Start with this line.
JavaScript
createTreeWalker(root, whatToShow?, filter?)
Call
What it does
createTreeWalker(root, whatToShow?, filter?)
Primary signature.
document.createTreeWalker(...)
Call with the arguments your case needs.
Built into the browser. Call createTreeWalker(...) on document — no import required.
Same root, two masks. One shows all nodes; one shows elements.
SHOW_ALL. The first visible child is often a text node (MDN).
SHOW_ELEMENT. Text is invisible, so firstChild() returns SPAN.
Tree moves matter. This is why TreeWalker is more than a flat iterator.
Compatibility
Browser Support
Document.createTreeWalker() is Baseline Widely available (MDN). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline · Widely available
Document.createTreeWalker()
Supported in every modern browser. A few legacy NodeFilter SHOW_* flags are inert — ignore them.
99%Modern browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
document.createTreeWalker()Excellent
Bottom line: Safe everywhere. Focus on whatToShow, filter semantics, and whether you need tree moves versus createNodeIterator.
Frequently Asked Questions
MDN: Document.createTreeWalker() returns a newly created TreeWalker object rooted at a given Node, optionally filtered by whatToShow and a filter callback.
No. MDN marks Document.createTreeWalker() as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard. A few NodeFilter SHOW_* flags are Legacy and no longer effective.
A new TreeWalker object (MDN). TreeWalker.currentNode starts as the root you passed in.
Both walk a filtered subtree. TreeWalker also supports tree-shaped moves such as parentNode, firstChild, lastChild, previousSibling, and nextSibling. Prefer createNodeIterator for a simple forward nextNode loop.
MDN: an optional bitmask from NodeFilter constants (for example SHOW_TEXT or SHOW_ELEMENT). It defaults to SHOW_ALL (0xFFFFFFFF).
MDN: FILTER_ACCEPT includes the node; FILTER_REJECT excludes that node and its whole subtree; FILTER_SKIP excludes only that node (descendants may still be visited).