Skip to content

Instantly share code, notes, and snippets.

@matthewmorrone
Last active August 29, 2015 14:10
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 matthewmorrone/815f2739237b81795fbd to your computer and use it in GitHub Desktop.
Save matthewmorrone/815f2739237b81795fbd to your computer and use it in GitHub Desktop.
This is an attempt to make jQuery's event helper functions use the "live" or "delegate" implementation of the on function, so that you don't have to run your event handler creators again if/when you add more elements.
$.each(("blur focus focusin focusout load resize scroll unload click dblclick " +
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
"change select submit keydown keypress keyup error contextmenu").split(" "),
function(i, name)
{
$.fn[name] = function(fn) {
return arguments.length > 0 ?
$(document).on(name, this, fn) :
this.trigger(name);
};
});
// Unfortunately, it doesn't quite work yet, as I believe the second parameter
// requires a CSS selector string, which as far as I know is not trivial
// to retrieve from an arbitrary jquery object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment