Skip to content

Instantly share code, notes, and snippets.

@rich97
Created July 4, 2011 15:40
Show Gist options
  • Select an option

  • Save rich97/1063504 to your computer and use it in GitHub Desktop.

Select an option

Save rich97/1063504 to your computer and use it in GitHub Desktop.
function getElementsByClassName(node,classname) {
if (node.getElementsByClassName) {
return node.getElementsByClassName(classname);
} else {
document.getElementsByClassName = function( className, nodeName ) {
var result = [], tag = nodeName||'*', node, seek, i;
if( document.evaluate ) {
seek = '//'+ tag +'[@class="'+ className +'"]';
seek = document.evaluate( seek, document, null, 0, null );
while((node = seek.iterateNext())) {
result.push( node );
}
} else {
var rightClass = new RegExp( '(^| )'+ className +'( |$)' );
seek = document.getElementsByTagName( tag );
for (i=0; i<seek.length; i++) {
if(rightClass.test((node = seek[i]).className)) {
result.push(seek[i]);
}
}
}
return result;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment