Skip to content

Instantly share code, notes, and snippets.

@d33pfri3d
Created April 27, 2012 10:24
Show Gist options
  • Save d33pfri3d/2508209 to your computer and use it in GitHub Desktop.
Save d33pfri3d/2508209 to your computer and use it in GitHub Desktop.
Micro $ Selector
var $ = function (d) {
var $ = document.querySelectorAll.bind(document);
Element.prototype.on = Element.prototype.addEventListener;
NodeList.prototype.on = function (event, fn) {
[].forEach.call(this, function (el) {
el.on(event, fn, false);
});
};
Element.prototype.trigger = function (type, data) {
var event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.data = data || {};
event.eventName = type;
this.dispatchEvent(event);
};
NodeList.prototype.trigger = function (event) {
[].forEach.call(this, function (el) {
el.trigger(event);
});
};
return function (s) {
var r = $(s);
return r.length === 1 ? r[0] : r;
};
}(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment