I want a shortcut notation for unbind('click'. I want .unclick to be unbind('click'.
+2
A:
jQuery.fn.extend({
unclick: function() {
return this.unbind('click');
}
});
Daniel Moura
2009-06-24 19:08:09
+1 for calling extend
Josh Stodola
2009-06-24 19:20:43
Nope, no benefit in using extend()... Just more characters.
J-P
2009-06-24 19:40:55
+4
A:
I think you want something like this:
$.fn['unclick'] = function(){
return this.unbind('click');
};
Now the following two lines are equivalent:
$(...).unbind('click');
$(...).unclick();
For all events (list of events copied from the jQuery source):
var events = ('blur,focus,load,resize,scroll,unload,click,dblclick,' +
'mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,' +
'mouseleave,change,select,submit,keydown,keypress,keyup,error'
).split(',');
jQuery.each(events, function(i, name){
jQuery.fn['un' + name] = function(){
return this.unbind(name);
};
});
Stephan202
2009-06-24 19:08:19
+1 That looks pretty good, but think you meant unclick as opposed to unlick :)
karim79
2009-06-24 19:10:36
@Josh: to be honest I'm not aware of the difference between calling extend and what I'm doing (I'm a javascript novice). I would like to point of that the jQuery source employs both techniques. Can you explain the benefits of calling extend?
Stephan202
2009-06-24 19:35:40
@Stephan202: Extend is the way jQuery wants it. Theoretically it's the same, but depending on the implementation of jQuery it could also differ. (What happens if there is already a unlink function etc.?)
Georg
2009-06-24 19:39:44
There's no difference. Using jQuery.extend() is only useful for adding multiple plugins.
J-P
2009-06-24 19:40:11