Skip to content

Instantly share code, notes, and snippets.

@LiamChapman
Last active January 1, 2016 23:59
Show Gist options
  • Select an option

  • Save LiamChapman/8220065 to your computer and use it in GitHub Desktop.

Select an option

Save LiamChapman/8220065 to your computer and use it in GitHub Desktop.
Shorthand cross-browser code for binding events to elements e.g. document.getElementById('#foo').on('click', function () { alert('hi!'); });
Element.prototype.on = function (evnt, func) {
if (this.addEventListener) // W3C DOM
this.addEventListener(evnt, func, false);
else if (this.attachEvent) { // IE DOM
var r = this.attachEvent("on"+evnt, func);
return r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment