JavaScript provides many methods to interact with the HTML DOM. We can use them to disable a button.
To disable a button in JavaScript, we get its reference and then set its disable
property to true
.
document.getElementById("Provide id of the button").disabled = true;
Let's consider a coding example of this.
In the code above, we created two buttons. We click the second button to disable or enable the first button, as follows:
btn1
and btn2
variables, respectively.isDisabled
variable that stores the state of the button to be disabled or enabled.