Sorry, I did misunderstand. I thought your intent was to create a custom event. As for the hooking of functions, it seems like a bad idea, in general. Everytime you go to remove an attribute now, jQuery has to step through some additional code which it really shouldn't.
Why not just do something like: $('#elem').removeClass('buttonDisabled').removeAttribute('disabled'); On Jan 9, 4:38 pm, kape <erlend.so...@gmail.com> wrote: > I'm not sure I understand your approach, however, it seems like > getting it to work would mean calling $('#elem').enable and $ > ('#elem').disable, which are not $('#elem').removeAttr('disable'), $ > ('#elem').attr('disabled', ''), or $('#elem').attr('disabled', '') so > your approach is not transparent. > > I can improve on my earlyer post as follows: > > var superRemoveAttr = $.fn.removeAttr; > $.fn.removeAttr = function(name) { > if(name == 'disabled') { > this.each(function() { > return $(this).hasClass('buttonDisabled'); > }).removeClass('buttonDisabled'); > } > superRemoveAttr.call(this, name); > }; > > But again, I think there should be a better way without overwriting > the jQuery functions. > > On Jan 9, 2:44 pm, Eric Garside <gars...@gmail.com> wrote: > > > $.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?