Skip to content

Instantly share code, notes, and snippets.

@williammalo
Forked from 140bytes/LICENSE.txt
Last active October 6, 2015 09:58
Show Gist options
  • Save williammalo/2976024 to your computer and use it in GitHub Desktop.
Save williammalo/2976024 to your computer and use it in GitHub Desktop.
set element properties on nodelists

a neat function that allows this:

document.getElementsByTagName('i').innerHTML="pie";

and it will change the innerHTML of all tags AUTOMATICALLY!!!!

incredible!

demo: http://jsfiddle.net/Vw54L/

function(z){
Object.defineProperty(
NodeList.prototype,z,{
set:function(a,b){ //make a setter function for when property is set on a Nodelist
for(b in this) //for every element in the nodelist...
if(b=this[b])b[z]=a //set property on element
}
}
)
}
function(z){Object.defineProperty(NodeList.prototype,z,{set:function(a,b){for(b in this)if(b=this[b])b[z]=a}})}
{
"name": "NodeListProp",
"description": "set element properties on nodelists",
"keywords": [
"dom",
"nodelist",
"properties"
]
}
<!DOCTYPE html>
<title>Foo</title>
<i>cake</i>
<i>cake</i>
<i>cake</i>
<i>cake</i>
<i>cake</i>
<script>
(
function(z){Object.defineProperty(NodeList.prototype,z,{set:function(a,b){for(b in this)if(b=this[b])b[z]=a}})}
)("innerHTML");
document.getElementsByTagName('i').innerHTML="pie";
document.getElementsByTagName('i')[1].style.color="red";
</script>​
@xpansive
Copy link

xpansive commented Jul 3, 2012

function(z){Object.defineProperty(NodeList.prototype,z,{set:function(a,b){for(b in this)if(b=this[b])b[z]=a}})}

@williammalo
Copy link
Author

@xpansive

Good idea! Thanks!

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