Tuesday, July 12, 2011

jQuery remove disabled attribute

I recently upgraded from jQuery 1.5.1 to 1.6.2. This upgrade affected code I have been using to enable or disable an element (such as a button).

Original Code

// disable
$("#btnSubmit").attr('disabled', 'disabled');

// enable
$("#btnSubmit").attr('disabled', '');

The above code worked in all major browsers until I upgraded to jQuery 1.6.2; however, after upgrading the element would not re-enable again by simply setting the disabled attribute to an empty string.

Solution

// disable
$("#btnSubmit").attr('disabled', 'disabled');

// enable
$("#btnSubmit").removeAttr('disabled');

Alternate Solution

// disable
$("#btnSubmit").attr('disabled', true);

// enable
$("#btnSubmit").attr('disabled', false);

I chose to go with the removeAttr solution since that seems more likely to eliminate differences from one browser to the next.

0 comments: