Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 25, 2012 12:55
Show Gist options
  • Save stefanocudini/2193492 to your computer and use it in GitHub Desktop.
Save stefanocudini/2193492 to your computer and use it in GitHub Desktop.
get element by class name
function getElementsByClassName(classname, node) {
if(!node) node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment