Sometimes, we want to disable submit button once it has been clicked with JavaScript.
In this article, we’ll look at how to disable submit button once it has been clicked with JavaScript.
How to disable submit button once it has been clicked with JavaScript?
To disable submit button once it has been clicked with JavaScript, we set its disabled
property to true
.
For instance, we write
<form action="/" method="POST">
<input name="txt" type="text" required />
<button id="btn" type="submit">Post</button>
</form>
to add a form.
Then we write
const form = document.querySelector("form");
form.onsubmit = () => {
const btn = document.getElementById("btn");
btn.disabled = true;
btn.innerText = "Posting...";
};
to select the form with querySelector
.
Then we set its onsubmit
property to a function that disables the button when we click the Post button.
In it, we select the button with getElementById
.
Then we set its disabled
property to true
to disable the button.
Conclusion
To disable submit button once it has been clicked with JavaScript, we set its disabled
property to true
.