Skip to content

Instantly share code, notes, and snippets.

@trevorparscal
Created February 26, 2014 22:41
Show Gist options
  • Save trevorparscal/9240288 to your computer and use it in GitHub Desktop.
Save trevorparscal/9240288 to your computer and use it in GitHub Desktop.
/**
* Capture event.
*
* Only works in IE9+.
*
* @todo Rework this so you can uncapture - which is tricky since
* we are wrapping the callback in a fixup function.
*
* @param {string} DOM event to capture
* @param {Function} Callback to execute when event is triggered
*/
$.fn.capture = function ( event, callback ) {
function fixup( e ) {
return callback( jQuery.event.fix( e ) );
}
return this.each(
function () {
var key, events;
if ( typeof event === 'string' ) {
events = {};
events[event] = callback;
} else if ( $.isPlainObject( event ) ) {
events = event;
}
if ( events ) {
for ( key in events ) {
this.addEventListener( key, fixup, true );
}
}
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment