Skip to content

Instantly share code, notes, and snippets.

@djalmajr
Created May 17, 2016 22:33
Show Gist options
  • Save djalmajr/b32b92f6616d942bf0311448d1965513 to your computer and use it in GitHub Desktop.
Save djalmajr/b32b92f6616d942bf0311448d1965513 to your computer and use it in GitHub Desktop.
getElementsByAttr
function getElementsByAttr(attribute, value, parent) {
var matchingElements = [],
allElements = (parent || document).getElementsByTagName("*");
for (var i = 0, n = allElements.length; i < n; i++) {
if (allElements[i].getAttribute(attribute) === value) {
// Element exists with attribute. Add to array.
matchingElements.push(allElements[i]);
}
}
return matchingElements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment