Created
September 29, 2012 19:13
-
-
Save kylewlacy/3804965 to your computer and use it in GitHub Desktop.
micro.js — A micro Javascript toolkit, for use when you miss the comfort of jQuery in vanilla JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Element.prototype.$ = function(query) { return this.querySelectorAll(query); }; | |
Window.prototype.$ = function(query) { return document.querySelectorAll(query); }; | |
Element.prototype.attr = function(attribute, value) { | |
if(value != undefined) { this.setAttribute(attribute, value); } | |
else { return this.getAttribute(attribute); } | |
}; | |
Element.prototype.indexWithin = function() { | |
var nodes = this.parentNode.childNodes; | |
for(node in nodes) { if(nodes[node] == this) { return node }; } | |
return -1; | |
}; | |
Element.prototype.insertAfter = function(new_node, existing_node) { | |
index = existing_node.indexWithin(); | |
siblings = this.childNodes.length; | |
if(index+1<siblings) { this.insertBefore(new_node, this.childNodes[index+1]); } | |
else { this.appendChild(new_node); } | |
}; | |
Element.prototype.prependChild = function(node) { this.insertBefore(node, this.childNodes[0]); }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment