Page 1 of 1

How to disable button after it's pressed?

Posted: Fri May 27, 2005 6:04 pm
by voltrader
I've seen this done before.

I'd like to disable the button after it's pressed to prevent multiple submissions. Is there a JS script to do this?

Posted: Fri May 27, 2005 6:17 pm
by timvw
long live websearches, for example i searched for "html button disable"


http://www.quirksmode.org/js/disabled.html

Posted: Fri Jun 03, 2005 7:16 pm
by voltrader
I've been experimenting with adding this bit of code in the input type=submit line:

Code: Select all

onClick=&quote;this.disabled=true;return true;&quote;
It does indeed disable the form button after its pressed, but I found that even though other POSTed data was sent to the server, the submit line itself is not.

This I gathered from Tim's link above.

Any ideas for a work-around?

Posted: Sat Jun 04, 2005 5:56 am
by R0d Longfella
If you want to make sure that the data is only send once, then why don't you use onSubmit?

Code: Select all

<script>
var bSubmitOnlyOnce = false;

function testSubmitOnlyOnce () {
  if (bSubmitOnlyOnce) {
    // alert ("Data already submitted!");
    return false;
  }
  bSubmitOnlyOnce = true;
  return true;
}
</script

<form onSubmit="return testSubmitOnlyOnce();">
<input type=submit name="save" value="Submit Data">
</form>
You could even put in an extra alert.

Posted: Mon Jun 06, 2005 12:32 pm
by voltrader
Thanks, that works great!

Posted: Thu Aug 23, 2007 5:33 pm
by VladSun
voltrader wrote:I've been experimenting with adding this bit of code in the input type=submit line:

Code: Select all

onClick=&quote;this.disabled=true;return true;&quote;
It does indeed disable the form button after its pressed, but I found that even though other POSTed data was sent to the server, the submit line itself is not.

This I gathered from Tim's link above.

Any ideas for a work-around?
Indeed, disabled form items are not send. A simple solution is to use hidden fields instead sending the value of the submit button.

Posted: Fri Aug 24, 2007 7:25 am
by superdezign
A more user-friendly way of doing that is to force a submission in the function, then disable the button so the users are at least aware. I, personally, don't condone doing this client-side though, because a user can always press the stop button. Sometimes the processing is slow, and trying again speeds it up.

Posted: Fri Aug 24, 2007 9:54 am
by feyd
This thread is two years dead... :|

Posted: Fri Aug 24, 2007 9:56 am
by VladSun
feyd wrote:This thread is two years dead... :|
Yeah, I've noticed it, but too late ... It is a link in one of the recent threads ... sorry :)