Skip to content

Instantly share code, notes, and snippets.

@kgn
Created October 6, 2010 06:13
Show Gist options
  • Select an option

  • Save kgn/612908 to your computer and use it in GitHub Desktop.

Select an option

Save kgn/612908 to your computer and use it in GitHub Desktop.
Get html elements by class name.
/*
There are many scripts out there for this but
many of them use a regular expression with \b.
The problem with this is \b matches more then
white space, like '-'. So if you want to find
all elements with the class 'expando' elements
with 'expando-button' will be returned.
This function fixes this problem.
*/
function getElementsByClassName(node, classname){
var a = [];
var re = new RegExp(' ' + classname + ' ');
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