# Prevent Double Form Submissions

> Disables a form's submit button and shows "Sending…" right after it is clicked, so impatient visitors cannot send comments, enquiries or orders twice.

- Type: JavaScript
- Runs: Site-wide footer
- Category: [Frontend & Design](https://wpide.com/library/category/frontend/)
- Tags: forms, usability, spam
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/frontend-prevent-double-submit/

## Code

```javascript
document.addEventListener('submit', function (e) {
    var form = e.target;
    if (!form || form.nodeName !== 'FORM' || form.classList.contains('wpide-allow-resubmit') || e.defaultPrevented) { return; }
    if (form.checkValidity && !form.checkValidity()) { return; }
    var buttons = form.querySelectorAll('button[type="submit"], input[type="submit"], button:not([type])');
    // Wait a tick so the clicked button's name and value are still sent with the form.
    setTimeout(function () {
        buttons.forEach(function (btn) {
            btn.disabled = true;
            btn.setAttribute('aria-disabled', 'true');
            btn.setAttribute('data-wpide-label', btn.nodeName === 'INPUT' ? btn.value : btn.textContent);
            if (btn.nodeName === 'INPUT') { btn.value = 'Sending…'; } else { btn.textContent = 'Sending…'; }
        });
    }, 0);
    setTimeout(function () {
        buttons.forEach(function (btn) {
            btn.disabled = false;
            btn.removeAttribute('aria-disabled');
            var label = btn.getAttribute('data-wpide-label');
            if (btn.nodeName === 'INPUT') { btn.value = label; } else { btn.textContent = label; }
        });
    }, 8000);
});
```

## Notes

Forms that submit with AJAX re-enable the button after 8 seconds. Add the class wpide-allow-resubmit to a form to skip it.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Prevent Double Form Submissions" in its snippet library and add it in one click; or create a new JavaScript snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
