Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created December 13, 2011 19:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/1473536 to your computer and use it in GitHub Desktop.
Save Raynos/1473536 to your computer and use it in GitHub Desktop.
All the DOM recursion you'll ever need
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)
}
}
}
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