Skip to content

Instantly share code, notes, and snippets.

@JacksonGariety
Forked from 140bytes/LICENSE.txt
Last active December 23, 2015 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacksonGariety/6605361 to your computer and use it in GitHub Desktop.
Save JacksonGariety/6605361 to your computer and use it in GitHub Desktop.

getElementsByClassName() polyfill

A polyfill for HTML5's getElementsByClassName() method. In only 126 bytes.

Does not work in:
  • IE < 5
  • FF > 20

Uses deprecated document.all

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function (e,t,r) { // `e` is our class name
t = []; // `t` is an array to be filled with elements
n = document.all; // `n` is a NodeList of all elements
for (r = n.length; r--;) { // for each node in `n`...
(" " + n[r].className + " ").indexOf(" " + e + " ") > -1 && t.push(n[r]); // push into `t` if has class `e`
} // braces only present in annotated.js
return t // return `t` with elements of class `e`
}
function(e,t,r){t=[],n=document.all;for(r=n.length;r--;)(" "+n[r].className+" ").indexOf(" "+e+" ")>-1&&t.push(n[r]);return t}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "getElementsByClassNameTinyfill",
"description": "A polyfill for HTML5's `getElementsByClassName()` method. In only 126 bytes.",
"keywords": [
"getElementsByClassName",
"HTML5",
"polyfill",
"tinyfill",
"codegolf"
]
}
<!DOCTYPE html>
<title>getElementsByClassName in 126 bytes</title>
<div class="getMe">You got me!</div>
<script>
var getElementsByClassName = function(e){var t=[],n=document.all;for(var r=n.length;r--;)(" "+n[r].className+" ").indexOf(" "+e+" ")>-1&&t.push(n[r]);return t}
console.log(getElementsByClassName("getMe"))
</script>
@atk
Copy link

atk commented Sep 19, 2013

My version (https://gist.github.com/atk/5764611) doesn't rely on document.all. Also, your version leaks the variable "n"; you can save some types if you use ~foo.indexOf(bar) instead of foo.indexOf(bar)>-1:

function(e,t,r,n){for(t=[],n=document.all,r=n.length;r--;)~(" "+n[r].className+" ").indexOf(" "+e+" ")&&t.push(n[r]);return t}

Also, if you use the RegExp approach of my version (which will not work on classnames that contain chars not allowed there anyway), you can save another 2 bytes:

function(e,t,r,n){for(t=[],n=document.all,r=n.length;r--;)eval('/\\b'+e+'\\b/').test(n[r].className)&&t.push(n[r]);return t}

@JacksonGariety
Copy link
Author

@atk I don't believe your version doesn't follow the rules of 140 bytes. (correct me if I'm wrong here)

The function in index.js should be anonymous and assignable to any variable. Yours is self-referential, no?

And thanks for the n variable leakage, will fix.

@atk
Copy link

atk commented Oct 30, 2013

Self-referential functions are allowed, as much as Jed and I are concerned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment