Terlin
Terlin

Reputation: 71

How to disable a button to avoid multiple submission?

document.getElementById('confirm_save').onclick = function() {  
    this.disabled = true;
}

I have used the above code but still the button is not disabled

Upvotes: 1

Views: 65

Answers (3)

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

Your JS code is valid but you need to add type="button" to your button so it will not act as a submit button and refresh the page returning to the initial status.

document.getElementById('confirm_save').onclick = function() {
  this.disabled = true;
}
<button type="button" id="confirm_save">Confirm save</button>

Upvotes: 2

Sir Catzilla
Sir Catzilla

Reputation: 321

Maybe create a variable at the start associated with the button and disable it as such:

let btn = document.getElementById("confirm_save");

btn.addEventListener("click", function() {
  btn.disabled = true;
});

Hope this helps;

Upvotes: 1

Michał Tkaczyk
Michał Tkaczyk

Reputation: 736

this should work:

document.getElementById('confirm_save').disabled = true;

Upvotes: 2

Related Questions