Prevent Double Submit with JavaScript 1

Prevent Double Submit with JavaScript

var form = document.getElementById('formID');
var submitButton = document.getElementById('submitID');

form.addEventListener('submit', function() {

   // Disable the submit button
   submitButton.setAttribute('disabled', 'disabled');

   // Change the "Submit" text
   submitButton.value = 'Please wait...';
			
}, false);

Here is what the above code is Doing:
1. We’re getting the form and submit button elements.
2. We’re adding an event listener to the form’s submit event.
3. When the form is submitted, we’re disabling the submit button.
4. We’re changing the submit button’s text to “Please wait…”.

The above code will work in all modern browsers.

If you want to support older browsers, you can use the following code:

Similar Posts