Skip to content

Instantly share code, notes, and snippets.

@williammalo williammalo/README.md forked from 140bytes/LICENSE.txt
Last active Oct 6, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

commented Jul 3, 2012

@xpansive

Good idea! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.