Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
element methods on nodelists

This function allows element methods to be used on nodelists, kinda like what jQuery does.

Object.getOwnPropertyNames(Element.prototype).forEach(function(a){ //for each element method
NodeList.prototype[a]=function(){ //make a nodelist method
for(var n=this.length;n--;)this[n][a].apply(this[n],arguments) //which wraps the element method in a for/in loop that applies the method to every element in the nodelist
;return this}
});
Object.getOwnPropertyNames(Element.prototype).forEach(function(a){NodeList.prototype[a]=function(){for(var n=this.length;n--;)this[n][a].apply(this[n],arguments);return this}})
{
"name": "NodeListMethodizr",
"description": "Allows element methods to be used on nodelists",
"keywords": [
"Nodelist",
"Element",
"Method",
"JQuery"
]
}
<!DOCTYPE html>
<title>Foo</title>
<i>foo bar lorem ipsum</i>
<i>foo bar lorem ipsum</i>
<i>foo bar lorem ipsum</i>
<i>foo bar lorem ipsum</i>
<i>foo bar lorem ipsum</i>
<i>foo bar lorem ipsum</i>
<script>
Element.prototype.css=function(a,i,n){for(n in''+a===a||a)this.style[n]=a[n];return i||n?(this.style[a]=i,this):getComputedStyle(this)[a]};
Object.getOwnPropertyNames(Element.prototype).forEach(function(a){NodeList.prototype[a]=function(){for(var n=this.length;n--;)this[n][a].apply(this[n],arguments);return this}})
document.querySelectorAll("i").css({color:"red"}).css({color:"blue"})
</script>
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.