Forum Moderators: open

Message Too Old, No Replies

Disable submit button OnClick

Wishing to prevent any repeated sendings of the form after one

         

Flurpal

5:31 pm on Jan 26, 2010 (gmt 0)

10+ Year Member



Hello,

I need some short snippet of code which will disable the Submit button instantly, once it's clicked. This way, people won't be able to resend the form or click it multiple times. I'm a noob at javascript, so, yeah. Thanks.

rocknbil

8:04 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The **best** idea is to craft your server side programming to error trap and handle double submits, if JS is disabled the below falls down. But it can be enhanced like so.

Inline cobble:

<input type="submit" onclick="this.disabled=true;" value="Submit">

Better would be to put the event handler on the form, using onsubmit to trigger some form validation function, then at the end of that function,

document.getElementById('submitButton').disabled=true;

<input type="submit" id="submitButton" value="Submit">

Take care not to ID the button reserved words, like "submit".

Fotiman

8:10 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I agree with what rocknbil wrote, except I would disable the button as the first action in the validation function, then re-enable it if validation failed. Also note, Firefox will remember the state of your buttons if you try to go back to this page, so setting the submit button to disabled can cause problems. One option is to set an unload event handler, which will prevent Firefox from caching the state.