Skip to content

Instantly share code, notes, and snippets.

@loktar00
Created September 12, 2011 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loktar00/1211640 to your computer and use it in GitHub Desktop.
Save loktar00/1211640 to your computer and use it in GitHub Desktop.
Get elements by classname cross browser
function byClass(matchClass){
if (!document.getElementsByClassName){
var elements = document.getElementsByTagName('*'),
i=0,
nodeList = [],
reg = new RegExp('(^|\\s)' + matchClass + '(\\s|$)')
for (i=0; i < elements.length;i++)
{
if(elements[i].className.match(reg) !== null){
nodeList.push(elements[i]);
}
}
return nodeList;
}else{
return document.getElementsByClassName(matchClass);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment