Last active
March 16, 2016 22:33
-
-
Save sebjwallace/a4669d4d257bd80a182f to your computer and use it in GitHub Desktop.
dom traversal with callback option on each node - http://jsbin.com/giluzi/edit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://jsbin.com/giluzi/edit | |
const traverseElement = (node,callback) => { | |
let level = 0; | |
const traverse = (node) => { | |
if(!node.nodeName) return; | |
level++; | |
if(callback) callback(node,level); | |
for(let child in node.children){ | |
traverse(node.children[child]); | |
} | |
level--; | |
} | |
traverse(node); | |
} | |
traverseElement(document.getElementById('root'), | |
(node,level) => { | |
console.log(level + ': ' + node.nodeName); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment