Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created June 14, 2020 23:26
Show Gist options
  • Save karol-majewski/bc498954c6ee1cede0a963b6630437a2 to your computer and use it in GitHub Desktop.
Save karol-majewski/bc498954c6ee1cede0a963b6630437a2 to your computer and use it in GitHub Desktop.
Depth-first traversal of a DOM subtree
function visit(node: Element, callback: (element: Element) => void): void {
callback(node);
Array
.from(node.children)
.forEach(child => visit(child, callback));
};
visit(document.body, console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment