Skip to content

Instantly share code, notes, and snippets.

@parkerproject
Created July 30, 2013 04:01
Show Gist options
  • Save parkerproject/6110126 to your computer and use it in GitHub Desktop.
Save parkerproject/6110126 to your computer and use it in GitHub Desktop.
getElementsByClassName function
function getElementsByClassName(node,classname) {
if (node.getElementsByClassName) { // use native implementation if available
return node.getElementsByClassName(classname);
} else {
return (function getElementsByClass(searchClass,node) {
if ( node === null )
node = document;
var classElements = [],
els = node.getElementsByTagName("*"),
elsLen = els.length,
pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
})(classname, node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment