public
Created

All the DOM recursion you'll ever need

  • Download Gist
iterateWalk.js
JavaScript
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)
}
}
}
recursivelyWalk.js
JavaScript
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
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.