Created
September 5, 2012 19:00
-
-
Save iamdustan/3642601 to your computer and use it in GitHub Desktop.
Tiny event delegation and event binding by extending Element with Element.prototype.on. Depends upon `matchesSelector`. Polyfill here: https://gist.github.com/3062955
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
(function(El) { | |
El.on = function(type, el, callback) { | |
if ('function' === typeof el) { | |
callback = el | |
el = null | |
} | |
this.addEventListener(type, function (e) { | |
if (!el || e.target.matchesSelector(el)) | |
return callback(e) | |
}, false) | |
} | |
}(window.Element.prototype)); |
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
(function(El){El.on=function(type,el,callback){if('function'===typeof el){callback=el;el=null}this.addEventListener(type,function(e){if(!el||e.target.matchesSelector(el))return callback(e)},false)}}(window.Element.prototype)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment