.

Another blindingly obvious way to disable a submit button when pressed

Probably the simplest and least problematic way to disable a submit button when pressed.

  1. # put this in your ‘application_helper’ file
  2.  
  3. def commit_tag(title, html_options={})
  4.   submit_tag title, html_options.merge(:id=>’commit’, :onclick=>’$("commit").disabled=true;’)
  5. end

Then just use ‘commit_tag’ instead of ’submit_tag’ in your form.
Strictly speaking, you don’t really need to give the button an ‘id’, but I do because it has the bonus side effect of making it easy programmatically disable the button from RJS templates.

One reason this approach is superior (I think) to an AJAX call is that there is less of a chance that an over-enthusiastic user will be able to click the button again before the AJAX response renders.

4 Responses to “Another blindingly obvious way to disable a submit button when pressed”

  1. October 13th, 2006 | 3:50 pm

    I also use button disabling but what about the already built in :disable_with => “Disabling”

    Hit me up on AIM btw Kevin yUncleD

  2. October 17th, 2006 | 7:44 pm

    This is certainly a hell of a lot better than an alert box saying “Please, for the love of God, don’t press submit twice!”.

    However, it needs to be said that one should not rely on JavaScript to solve the double submit problem. You really need some server-side smarts to detect replays since the user could reload the page and resubmit the form.

  3. October 17th, 2006 | 10:12 pm

    True enough. You may really need a belt AND suspenders for this one. There are also some subtle side effects of this method. Notably, it becomes very difficult to determine which button was pressed if you have more than one.

  4. November 21st, 2006 | 1:39 pm

    Even more obvious would be “this.disabled=true;” ;-)
    But this “disabled” solution may prevent the form being submitted in Safari. (Maybe my Safari has a bad day today?)

    :disable_with => “Processing…” works like a breeze!

Leave a reply