Skip to content

Instantly share code, notes, and snippets.

@intelekshual
Created November 4, 2011 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intelekshual/1338462 to your computer and use it in GitHub Desktop.
Save intelekshual/1338462 to your computer and use it in GitHub Desktop.
getElementsByAttribute
function getElementsByAttribute(tag, name, value) {
var elements = (tag == "*" && document.all) ? document.all : document.getElementsByTagName(tag),
matches = [],
re = (typeof name !== "undefined") ? new RegExp("(^|\\s)" + value + "(\\s|$)", "i") : null,
current,
attribute;
for (var i = 0; i < elements.length; i++) {
current = elements[i];
attribute = current.getAttribute && current.getAttribute(name);
if (typeof attribute === "string" && attribute.length > 0) {
if(typeof value === "undefined" || (re && re.test(attribute))) {
matches.push(current);
}
}
}
return matches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment