Sometimes, we want to prevent multiple clicks on button with JavaScript.
In this article, we’ll look at how to prevent multiple clicks on button with JavaScript.
How to prevent multiple clicks on button with JavaScript?
To prevent multiple clicks on button with JavaScript, we disable it after the first click.
For instance, we write
<form>
<input />
<button
onclick="this.disabled = true; this.value = 'Submitting...'; this.form.submit();"
>
submit
</button>
</form>
to set the onclick
attribute of the button in the form to this.disabled = true; this.value = 'Submitting...'; this.form.submit();
to disable the button with this.disabled = true;
We set the button’s text to Submitting… with this.value = 'Submitting...'
.
Then we submit the form that the button is in with this.form.submit();
.
Conclusion
To prevent multiple clicks on button with JavaScript, we disable it after the first click.