Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Created September 5, 2012 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdustan/3642601 to your computer and use it in GitHub Desktop.
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
(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));
(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