How to disable a button in JavaScript?

To disable a button in JavaScript, do this: button.disabled = true.

For example, if this is your button:

<button id="my-button">Click Me</button>

Then you can disable it with this JavaScript:

document.getElementById('my-button').disabled = true;

How to disable a button based on condition?

const disabled = Math.random() < 0.5;

document.getElementById('my-button').disabled = disabled;