Skip to content

Instantly share code, notes, and snippets.

@pdokas
Created December 1, 2010 19:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pdokas/724014 to your computer and use it in GitHub Desktop.
Save pdokas/724014 to your computer and use it in GitHub Desktop.
Add a context param to $.fn.bind()
$('h1').bind('click', function() {
this.log('clicked!');
}, console);
$('h2').bind('click', {msg: 'clicked!'}, function(e) {
this.log(e.data.msg);
}, console);
(function($) {
$.fn.oldbind = $.fn.bind;
$.fn.bind = function(type, data, handler, context) {
if (!$.isPlainObject(data)) {
context = handler;
handler = data;
data = {};
}
if (context) {
handler = $.proxy(handler, context);
}
this.oldbind(type, data, handler);
}
})(jQuery);
@pdokas
Copy link
Author

pdokas commented Dec 1, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment