Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  189] [ 7]  / answers: 1 / hits: 40653  / 7 Years ago, fri, february 3, 2017, 12:00:00

I have a Rails Devise form that has javascript validation. When the user presses submit, the validation works and the user is refocused on the form where they need to be.



However, rails uses the data-disable-with to disable the button after it has been clicked, so after the validation the user cannot click submit anymore. I am trying to set up some kind of listener to check when the button has been disabled, wait a little while to prevent double clicks, then re-enable the button.



I have tried many iterations of code, the latest I tried was:



      $(document.on(ajax:success, new_supplier, function() {
var button;

button = $(this).find('.btn-sign-up');
setTimeout((function() { return button.disabled=false; }),1);


});


But had no success, previously I tried just adding a listener to the button:



      var button = document.getElementById('btn-sign-up');
button.addEventListener(click, enableButton);

function enableButton() {
if (button.disabled)
{
window.setTimeout(enable(), 2000);
}
}


function enable() {
button.disabled=false;
}


But this failed because it ran before the function (hidden in rails ether) that disabled the button so did not work.



My submit button is a simple:



    <%= f.submit Create my account, class: 'btn-sign-up', id: 'btn-sign-up' %>


Can anyone help me out here? I think if I can disable the jQuery_ujs for this page that would work, but not sure if I can do that.


More From » jquery

 Answers
94

I managed to solve this quite simply. I just went in and removed the data-disable-with from the button with the code:



    $('#my-button').removeAttr('data-disable-with');


And then re-establishing the attribute when the form was ready to be submitted to prevent double clicks from creating duplicates.


[#59088] Wednesday, February 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
justynkaina

Total Points: 742
Total Questions: 83
Total Answers: 102

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;