Created
November 4, 2011 01:39
-
-
Save intelekshual/1338462 to your computer and use it in GitHub Desktop.
getElementsByAttribute
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
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