An example of when you'd want to do this is when you want to prevent a user from submitting a form until all the required fields are filled out.
A button has an internal state, similar to checkboxes and radio buttons.
This means we can manipulate the state of the button, and therefore disable it.
How to disable a button
The first thing you'll want to do is to get the button element.
If you have a class applied, you can get your button like this:
const button = document.querySelector(".button");
Now that you've gotten your button, simply set its
disabled property to
const button = document.querySelector(".button"); button.disabled = true;
Hopefully, this has been helpful to you.
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Leave us a message!