Skip to content

Instantly share code, notes, and snippets.

@kylewlacy
Created September 29, 2012 19:13
Show Gist options
  • Save kylewlacy/3804965 to your computer and use it in GitHub Desktop.
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
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