Skip to content

Instantly share code, notes, and snippets.

@naveed-fida
Created January 25, 2017 06:51
Show Gist options
  • Save naveed-fida/75ad78ff720f462555f60cc14836335f to your computer and use it in GitHub Desktop.
Save naveed-fida/75ad78ff720f462555f60cc14836335f to your computer and use it in GitHub Desktop.
Dom methods
// executes a callback for a dom element and all it's children
function walk(node, callback) {
callback(node);
node.childNodes.forEach(function(child) {
walk(child, callback);
});
}
// same functionality as the built-in one
function getElementsByTagName(tagName) {
var matches = [];
walk(document.body, function(node) {
if (node.nodeName.toLowerCase() === tagName) {
matches.push(node);
}
});
return matches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment