Created
January 25, 2017 06:51
-
-
Save naveed-fida/75ad78ff720f462555f60cc14836335f to your computer and use it in GitHub Desktop.
Dom methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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