Docs
19.1-Advanced-DOM-Traversal
19.1 Advanced DOM Traversal
Overview
Beyond basic DOM navigation, JavaScript provides powerful APIs for traversing and manipulating the DOM tree efficiently.
TreeWalker API
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TreeWalker Navigation β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββ β
β β root β β
β ββββββ¬βββββ β
β βββββββββββΌββββββββββ β
β βΌ βΌ βΌ β
β ββββββββββ ββββββββββ ββββββββββ β
β β child1 β β child2 β β child3 β β
β ββββββ¬ββββ ββββββββββ ββββββββββ β
β ββββββ΄βββββ β
β βΌ βΌ β
β ββββββββββ ββββββββββ β
β βgrandch1β βgrandch2β β
β ββββββββββ ββββββββββ β
β β
β TreeWalker Methods: β
β β’ parentNode() - Move to parent β
β β’ firstChild() - Move to first child β
β β’ lastChild() - Move to last child β
β β’ previousSibling() - Move to previous sibling β
β β’ nextSibling() - Move to next sibling β
β β’ nextNode() - Next in document order β
β β’ previousNode() - Previous in document order β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Node Filters
| Filter Constant | Value | Description |
|---|---|---|
| NodeFilter.SHOW_ALL | -1 | Show all nodes |
| NodeFilter.SHOW_ELEMENT | 1 | Element nodes only |
| NodeFilter.SHOW_TEXT | 4 | Text nodes only |
| NodeFilter.SHOW_COMMENT | 128 | Comment nodes only |
Basic TreeWalker
const walker = document.createTreeWalker(
document.body, // Root node
NodeFilter.SHOW_ELEMENT, // What to show
{
acceptNode: (node) => {
return node.classList.contains('target')
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP;
},
}
);
// Navigate the tree
while (walker.nextNode()) {
console.log(walker.currentNode);
}
Range API
// Create a range
const range = document.createRange();
// Select node contents
range.selectNodeContents(element);
// Set specific boundaries
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
// Extract contents
const fragment = range.extractContents();
Selection API
// Get current selection
const selection = window.getSelection();
// Get selected text
const text = selection.toString();
// Get selected range
const range = selection.getRangeAt(0);
// Modify selection
selection.removeAllRanges();
selection.addRange(newRange);
Custom Queries
// Find by custom attribute pattern
function findByDataPrefix(prefix) {
return document.querySelectorAll(`[data-${prefix}]`);
}
// Find text nodes containing string
function findTextContaining(text) {
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{
acceptNode: (node) =>
node.textContent.includes(text)
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT,
}
);
const results = [];
while (walker.nextNode()) {
results.push(walker.currentNode);
}
return results;
}
Summary
- β’TreeWalker for controlled DOM traversal
- β’NodeIterator for simpler linear traversal
- β’Range API for selecting DOM regions
- β’Selection API for user selections
- β’Combine filters for precise node finding