Skip to content

Instantly share code, notes, and snippets.

@getify
Created June 25, 2010 05:02
Show Gist options
  • Save getify/452439 to your computer and use it in GitHub Desktop.
Save getify/452439 to your computer and use it in GitHub Desktop.
function getElementsByClassName(classname,root,tagname) {
root = root || document;
tagname = tagname || "*";
classname = classname.split(/\s+/);
var els = root.getElementsByTagName(tagname),
ret=[], i,j=0,k,len = els.length, m, cn, klen=classname.length, reEdgeSpaces = /[\t\n\r\f]/g
;
for (i=0; i<len; i++) {
cn = ' '+els[i].className.replace(reEdgeSpaces,' ')+' ';
m = true;
for (k=0; k<klen; k++) {
if (cn.indexOf(' '+classname[k]+' ')<0) {
m = false;
break;
}
}
if (m) {
ret[j++] = els[i];
}
}
return ret;
}
@getify
Copy link
Author

getify commented Jun 25, 2010

thanks to jdalton for many hints/improvements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment