$.fn.enable = function(callback){
    return this.each(function(){
        if (callback) return $(this).data('enableCB', callback);
        if (typeof $(this).data('enableCB') == 'function') $(this).data
('enableCB')();
     });
}

$.fn.disable = function(callback){
    return this.each(function(){
        if (callback) return $(this).data('disableCB', callback);
        if (typeof $(this).data('disableCB') == 'function') $
(this).data('disableCB')();
     });
}

These functions aren't tested, but I'm pretty sure they will give you
what you want (simulating the event structure in jQuery).

If you call $('#elem').enable(function(){ /* Do something */ }); it
will set the event handler.
If you then call $('#elem').enable(), it will trigger the callback, if
one exists.

That help at all?

On Jan 9, 2:30 pm, kape <erlend.so...@gmail.com> wrote:
> I have created custom styled buttons in my page and would like to
> toggle their class and therefore their look when they get disabled or
> enabled.  So is there any way to call a function when .removeAttr
> ('disabled'), .attr('disabled', ''), and .attr('disabled', 'disabled')
> are invoked?  Basically, is there any way to have enable/disable act
> like an event such as click, mouseover, etc.?
>
> I've tried overwriting the jQuery function as follows:
>
>         jQuery.fn.removeAttr = function(name) {
>           if(this.eq(0).hasClass('buttonDisabled') && name ==
> 'disabled')
>             this.eq(0).removeClass('buttonDisabled');
>           jQuery.attr(this.get(0), name, "" );
>           if (this.get(0).nodeType == 1)
>             this.get(0).removeAttribute(name);
>         };
>
> and when $('.buttonDisabled').removeAttr('disabled') is called, the
> disabled attribute and buttonDisabled class are removed.  This is what
> I want, but it doesn't seem right.  I shouldn't be overwriting the
> removeAttr function.  Also, I'd have to do the same thing for jQuery's
> attr() function to add the buttonDisabled class when .attr('disabled',
> 'disabled') is called.  Can I add the code I need to the functions and
> then call "super" somehow?  Is there a simpler way to achieve what I
> want?

Reply via email to