public
Last active

Add a context param to $.fn.bind()

  • Download Gist
Example.js
JavaScript
1 2 3 4 5 6 7
$('h1').bind('click', function() {
this.log('clicked!');
}, console);
 
$('h2').bind('click', {msg: 'clicked!'}, function(e) {
this.log(e.data.msg);
}, console);
jquery-bind-with-context.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.