Created
April 27, 2012 10:24
-
-
Save d33pfri3d/2508209 to your computer and use it in GitHub Desktop.
Micro $ Selector
This file contains hidden or 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
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