Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

All the DOM recursion you'll ever need

View iterateWalk.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var slice = Array.prototype.slice
 
function iterativelyWalk(nodes, cb) {
nodes = slice.call(nodes)
 
while(nodes.length) {
var node = nodes.shift(),
ret = cb(node)
 
if (ret) {
return ret
}
 
if (node.childNodes.length) {
nodes = slice.call(node.childNodes).concat(nodes)
}
}
}
View iterateWalk.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function recursivelyWalk(nodes, cb) {
for (var i = 0, len = nodes.length; i < len; i++) {
var node = nodes[i],
ret = cb(node)
 
if (ret) {
return ret
}
 
if (node.childNodes.length) {
var ret = recursivelyWalk(node.childNodes, cb)
if (ret) {
return ret
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.