Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active October 12, 2015 08:08
Show Gist options
  • Save obenjiro/3997394 to your computer and use it in GitHub Desktop.
Save obenjiro/3997394 to your computer and use it in GitHub Desktop.
Smallest getElementsByClassName polyfill (146 chars)
//A little bit buggy, but for me it's ok (work wrong if \t (tab) is used inside of className field)
function getElementsByClassPolyfill(className, array, space, index, item){
index = 0, space = " ", className = space + className + space, array = [];
while(item = document.all[index++]) {
if((space + item.className + space).indexOf(className)+1) {
array.push(item);
}
}
return array;
}
//obfuscated (146 chars)
function getElementsByClassPolyfill(a,b,c,d,e){for(d=0,c=" ",a=c+a+c,b=[];e=document.all[d++];)(c+e.className+c).indexOf(a)+1&&b.push(e);return b}
//usage example
getElementsByClassPolyfill("test");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment