Re-enable links disabled with disable_with
Asked Answered
S

7

14

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

Springs answered 25/6, 2013 at 10:41 Comment(0)
C
14

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]'));
});
Cresida answered 13/7, 2015 at 21:44 Comment(0)
L
9

A solution I found here:

$(window).unload(function() {
  $.rails.enableFormElements($($.rails.formSubmitSelector));
});
Lazor answered 13/3, 2014 at 16:29 Comment(1)
Yup, 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' ) )Satirical
B
5

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);
})
Barbarian answered 11/5, 2017 at 21:19 Comment(0)
C
2

Hey its quite simple you just need to find button and do

$button = $('#someId')
$.rails.enableElement($button)
$button.removeAttr('disabled')
Cracked answered 1/6, 2017 at 23:54 Comment(1)
This doesn't change the text (i.e. value) of the button back to what it was before submission.Satirical
P
1

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]');
Presentiment answered 21/4, 2015 at 21:45 Comment(0)
P
0

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.

ref: https://github.com/rails/jquery-ujs/issues/357

Polypus answered 5/9, 2014 at 13:37 Comment(0)
L
-1

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

Luanaluanda answered 25/6, 2013 at 10:59 Comment(2)
Ah, interesting. Looking for something to change it back to its original state. That doesn't seem to do it.Springs
Like bevanb said. This does not change the element back to its original state.Jens

© 2022 - 2024 — McMap. All rights reserved.