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/2847394 to your computer and use it in GitHub Desktop.
Save williammalo/2847394 to your computer and use it in GitHub Desktop.
element coercion

Why?

Because why the hell not?

Allows you to use nodelist methods on strings!

It's kinda useless on it's own, but with https://gist.github.com/2847249 it's pretty fun =D

Object.getOwnPropertyNames(NodeList.prototype).forEach(function(a){
String.prototype[a]=function(){document.querySelectorAll(this)[a].apply(document.querySelectorAll(this),arguments)}
});
Object.getOwnPropertyNames(NodeList.prototype).forEach(function(a){String.prototype[a]=function(){document.querySelectorAll(this)[a].apply(document.querySelectorAll(this),arguments)}})
{
"name": "theNameOfYourLibWhichMustBeAValidCamelCasedJavaScriptIdentifier",
"description": "This should be a short description of your entry.",
"keywords": [
"five",
"descriptive",
"keywords",
"or",
"fewer"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div style=white-space:pre>
<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>
</div>​
<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 in this)this[n][a].apply(this[n],arguments)}
});
Object.getOwnPropertyNames(NodeList.prototype).forEach(function(a){
String.prototype[a]=function(){document.querySelectorAll(this)[a].apply(document.querySelectorAll(this),arguments)}
});
"div>i".css({color:"red"})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment