How can I manually re-enable links (not form elements) that get disabled with Rails' disable_with
feature?

- 8,201
- 10
- 53
- 90
7 Answers
The call to reenable links is slightly different than form elements. It actually binds a handler to the click event that stops anything else from happening. I was able to figure this out by investigating how the jquery-ujs library.
To reverse this effect, simply use the enableElement
method on your jQuery object:
$.rails.enableElement($('a[data-disable-with]'));
With Turbolinks, it also helps to watch for the 'page:change'
event instead of window.unload
:
$(document).on('page:change', function() {
$.rails.enableElement($('a[data-disable-with]'));
});

- 141
- 1
- 2
A solution I found here:
$(window).unload(function() {
$.rails.enableFormElements($($.rails.formSubmitSelector));
});

- 26,629
- 7
- 58
- 79
-
1Yup, this works in Rails 3.2. Essentially, just pass a jQuery object or objects to `$.rails.enableFormElements` and it will re-enable it. In my case I passed the form I was submitting: `$.rails.enableFormElements( $( 'div.modal form' ) )` – Joshua Pinter May 13 '18 at 01:43
Rails has updated their javascript to no longer use jQuery.
You can now re-enable elements with the following (assuming you are still using jQuery):
var selectors = [Rails.linkDisableSelector, Rails.formEnableSelector].join(', ');
$(selectors).each(function() {
Rails.enableElement(this);
})

- 2,006
- 21
- 13
Hey its quite simple you just need to find button and do
$button = $('#someId')
$.rails.enableElement($button)
$button.removeAttr('disabled')

- 603
- 6
- 13
-
2This doesn't change the text (i.e. `value`) of the button back to what it was before submission. – Joshua Pinter Jun 03 '19 at 22:19
Based on @DGM solution I ended up with the following code:
$.rails.enableFormElements($disabled_button);
Where:
$disabled_button
is the jQuery object for the button disabled by data-disable-with
which could be selected like this:
$disabled_button = $('[data-disable-with]');

- 19,102
- 6
- 69
- 67
OK I found this interesting work around (apparently the problem is only in FF) set :autocomplete => 'off' and now it works. Or one of the other answer might work as well.

- 62,887
- 36
- 269
- 388
You can use jQuery to remove the data-disable-with attribute that Rails adds to the button: $('#disabledbutton').removeAttr('data-disable-with');

- 55
- 4
-
1Ah, interesting. Looking for something to change it back to its original state. That doesn't seem to do it. – bevanb Jun 25 '13 at 11:16
-
Like bevanb said. This does not change the element back to its original state. – Peter Black May 28 '15 at 18:35