Skip to content

Instantly share code, notes, and snippets.

@williammalo
Forked from 140bytes/LICENSE.txt
Last active October 5, 2015 13:58
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 williammalo/2816659 to your computer and use it in GitHub Desktop.
Save williammalo/2816659 to your computer and use it in GitHub Desktop.
nodelists to arrays

This code transparently makes nodelists act like arrays!

With it, you can do this:

document.getElementsByTagName('a').forEach(function(a){

a.style.color = '#F00'

})​

(function(a,b,c,d){ //start of self-calling function
for(b in a=Object.getOwnPropertyNames(c=Array.prototype)) //for every array method
NodeList.prototype[a[b]]=c[a[b]] //add it to nodelists
})() //end of self-calling function
(function(a,b,c,d){for(b in a=Object.getOwnPropertyNames(c=Array.prototype))NodeList.prototype[a[b]]=c[a[b]]})()
{
"name": "listToArray",
"description": "Makes nodelists act like arrays",
"keywords": [
"nodelist",
"array",
"prototype",
"foreach"
]
}
<!DOCTYPE html>
<title>Foo</title>
​<a>lolol</a>
<a>efesgdsge</a>
<a>rdgsdrhrsd</a>
<a>rgdsgdsg</a>
<a>eswred</a>
<a>dfsdghtyj</a>
<a>srhdhthtj</a>
<script>
(function(a,b,c,d){for(b in a=Object.getOwnPropertyNames(c=Array.prototype))NodeList.prototype[a[b]]=c[a[b]]})()
document.getElementsByTagName('a').forEach(function(a){
a.style.color = '#F00'
})​
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment