Recently I've started working on a new Chrome extension in my free time and in the research on how to handle some of the functionalities, I've started discovering more and more functionalities that JS has in dealing with the DOM of the page.
Given how an overwhelming number of people using JS are using it only through a framework, this would make an interesting topic for a series of short articles for people to learn a bit more about the underlying technologies that the frameworks they rely on are actually using.
We've all been there — you need to find specific elements in the DOM, but querySelector
and getElementsBy*
aren't quite cutting it. Maybe you need to find all text nodes containing a specific phrase, or you want to traverse elements matching certain conditions while skipping others. Enter TreeWalker - a powerful but often overlooked DOM traversal API.
What is TreeWalker?
TreeWalker is a DOM interface that lets you efficiently traverse and filter DOM nodes. Think of it as a more powerful and flexible alternative to methods like querySelector
. While querySelector
gives you elements matching a CSS selector, TreeWalker lets you:
- Navigate the DOM tree in any direction (forward, backward, up, down)
- Filter nodes based on custom conditions
- Skip certain parts of the tree entirely
- Access text nodes directly (something
querySelector
can't do)
Creating a TreeWalker
Let's start with a basic example:
const walker = document.createTreeWalker(
document.body, // Root node to start traversal
NodeFilter.SHOW_TEXT, // Only show text nodes
{
acceptNode: function(node) {
// Only accept text nodes that aren't empty
return node.textContent.trim().length > 0
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
}
}
);
The three parameters are:
- Root node — where to start traversing
- What types of nodes to show (text, elements, comments, etc.)
- A filter function that decides which nodes to accept or reject
Real World Examples
1. Find and Replace Text
Here's something you'll actually use — finding and replacing text while preserving HTML structure.
function replaceText(root, search, replace) {
const walker = document.createTreeWalker(
root,
NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
return node.textContent.includes(search)
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
}
}
);
let node;
while (node = walker.nextNode()) {
node.textContent = node.textContent.replace(search, replace);
}
}
// Usage
replaceText(document.body, 'old text', 'new text');
This is much more efficient than using innerHTML and won't break event listeners or form input values.
2. Custom DOM Query
Need to find elements matching complex conditions? TreeWalker has you covered. Let's build something more complex — say you need to find all <span>
elements that contain specific text, but only if they're inside <div>
elements with a certain class, and ignore any that are inside <button>
elements:
function findElementsByComplexCondition(root, config) {
const results = [];
const walker = document.createTreeWalker(
root,
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
// Skip nodes we don't care about early
if (node.nodeType === Node.ELEMENT_NODE &&
node.tagName === 'BUTTON') {
return NodeFilter.FILTER_REJECT; // Skip button and its contents
}
// Check for matching span elements
if (node.nodeType === Node.ELEMENT_NODE &&
node.tagName === 'SPAN') {
// Check if parent is a div with required class
const parent = node.parentElement;
if (!parent ||
parent.tagName !== 'DIV' ||
!parent.classList.contains(config.parentClass)) {
return NodeFilter.FILTER_SKIP;
}
// Check if span contains the required text
const text = node.textContent?.toLowerCase() || '';
if (!text.includes(config.searchText.toLowerCase())) {
return NodeFilter.FILTER_SKIP;
}
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_SKIP;
}
}
);
let node;
while (node = walker.nextNode()) {
results.push(node);
}
return results;
}
This would match:
✅ Will match:
<div class="message-container">
<span>Error: Invalid input</span>
</div>
❌ Won't match all of the following:
<div class="other-container">
<span>Error: Invalid input</span>
</div>
<button>
<span>Error: Invalid input</span>
</button>
<div class="message-container">
<span>Success!</span>
</div>
[Rest of code examples...]
The Swiss Army Knife of DOM Traversal
TreeWalker isn't limited to forward traversal. You can move in any direction:
// Move to next node
walker.nextNode();
// Move to previous node
walker.previousNode();
// Move to first child
walker.firstChild();
// Move to last child
walker.lastChild();
// Move to parent
walker.parentNode();
When Should You Use TreeWalker?
TreeWalker shines when:
- You need to find text nodes (
querySelector
can't do this) - You have complex filtering requirements
- You need to traverse the DOM in a specific order
- Performance matters (TreeWalker is generally faster than recursive DOM traversal)
TypeScript Support
Good news for TypeScript users — the types are built right in:
interface TreeWalker {
readonly currentNode: Node;
readonly filter: NodeFilter | null;
readonly root: Node;
readonly whatToShow: number;
firstChild(): Node | null;
lastChild(): Node | null;
nextNode(): Node | null;
nextSibling(): Node | null;
parentNode(): Node | null;
previousNode(): Node | null;
previousSibling(): Node | null;
}
Final Thoughts
TreeWalker is one of those APIs that, once you know about it, you'll find yourself reaching for more often than you might expect. It's particularly useful for building accessibility tools, content management systems, or any application that needs to analyze or modify DOM content programmatically.
While it might seem complex at first, TreeWalker's power and flexibility make it worth adding to your toolkit. Next time you find yourself writing a recursive DOM traversal function, consider whether TreeWalker might be a better fit.
P.S. If you've made it this far, here's a pro tip: Combine TreeWalker with MutationObserver to create powerful DOM monitoring tools. But that's a topic for another article... 😉
If you found this article helpful, feel free to like and follow for more JavaScript tips and tricks.
Cover photo by Branko Stancevic on Unsplash
Cover photo by Branko Stancevic on Unsplash
Cat photo by Jan Gustavsson on Unsplash
Top comments (0)