Skip to content

Instantly share code, notes, and snippets.

@mikepenzin
Created February 28, 2017 17:47
Show Gist options
  • Save mikepenzin/713173f7a420423000d9a8eb907bc66e to your computer and use it in GitHub Desktop.
Save mikepenzin/713173f7a420423000d9a8eb907bc66e to your computer and use it in GitHub Desktop.
Depth-First-Search algorithm
// Create a function that, given a DOM Element on the page,
// will visit the element itself and all of its descendents
// (not just its immediate children). For each element visited,
// the function should pass that element to a provided callback function.
// The arguments to the function should be:
// a DOM element
// a callback function (that takes a DOM element as its argument)
function Traverse(p_element,p_callback) {
p_callback(p_element);
var list = p_element.children;
for (var i = 0; i < list.length; i++) {
Traverse(list[i],p_callback); // recursive call
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment