Skip to content

Instantly share code, notes, and snippets.

@williammalo
Forked from 140bytes/LICENSE.txt
Last active October 5, 2015 17:48
Show Gist options
  • Save williammalo/2847249 to your computer and use it in GitHub Desktop.
Save williammalo/2847249 to your computer and use it in GitHub Desktop.
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