Skip to content

Instantly share code, notes, and snippets.

@mgechev
Created November 26, 2014 16:37
Show Gist options
  • Save mgechev/5fbeca3627fef7bad0de to your computer and use it in GitHub Desktop.
Save mgechev/5fbeca3627fef7bad0de to your computer and use it in GitHub Desktop.
function getElementsByTagName(root, tagName) {
tagName = tagName.toUpperCase();
var stack = [root],
result = [],
current, node;
while (stack.length) {
current = stack.pop();
for (var i = 0; i < current.children.length; i += 1) {
node = current.children[i];
if (node.tagName.toUpperCase() === tagName) {
result.push(node);
}
stack.push(node);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment