Skip to content

Instantly share code, notes, and snippets.

@ingowennemaring
Created April 18, 2013 15:38
Show Gist options
  • Save ingowennemaring/5413724 to your computer and use it in GitHub Desktop.
Save ingowennemaring/5413724 to your computer and use it in GitHub Desktop.
The missing function "getElementsByClassName()"
function getElementsByClassName(class_name) {
var all_obj, ret_obj=new Array(), j=0, teststr;
if(document.all) {
all_obj = document.all;
}
else if(document.getElementsByTagName && !document.all){
all_obj = document.getElementsByTagName("*");
}
for(var i = 0; i < all_obj.length; i++) {
if(all_obj[i].className.indexOf(class_name) != -1) {
teststr = "," + all_obj[i].className.split(" ").join(",") + ",";
if(teststr.indexOf("," + class_name + ",") !=-1) {
ret_obj[j] = all_obj[i];
j++;
}
}
}
return ret_obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment