-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable Submit Button to Prevent Duplicate Submissions (#4275)
* A JS util to disable a submit button after click * Add a spinny boi, change button text while loading
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Attach an event handler to every button with attribute "disable-submit-after-click". | ||
* This will disable the button after clicking it, enable the sibling spinning icon | ||
* (if it exists), and submit the form. | ||
*/ | ||
function disableSubmitAfterClick() { | ||
const matching_buttons = document.querySelectorAll( | ||
'button[disable-submit-after-click]' | ||
); | ||
|
||
matching_buttons.forEach((button) => { | ||
const form = button.closest('form'); // Parent form | ||
const loader = button.parentElement.querySelector(`div[id='loader']`); // Sibling loader | ||
|
||
button.addEventListener('click', () => { | ||
button.disabled = true; | ||
button.textContent = 'Submitting...'; | ||
if (loader) { | ||
loader.hidden = false; | ||
} | ||
form.submit(); | ||
}); | ||
}); | ||
} | ||
|
||
function init() { | ||
disableSubmitAfterClick(); | ||
} | ||
|
||
window.onload = init; |