views:

496

answers:

2

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
+1 for calling extend
Josh Stodola
Nope, no benefit in using extend()... Just more characters.
J-P
+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
+1 That looks pretty good, but think you meant unclick as opposed to unlick :)
karim79
Indeed I did ;)
Stephan202
Shouldn't you be calling extend...?
Josh Stodola
@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
@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
There's no difference. Using jQuery.extend() is only useful for adding multiple plugins.
J-P