by Sai gowtham

How to disable the HTML button in JavaScript

Learn, how to disable or enable the button in JavaScript.

We mostly disabled the button when a text input element is empty or a checkbox is not checked in the signup forms.

Disabling the button

To disable the button, first we need to access the button element inside JavaScript using document.querySelector() method.

const btn = document.querySelector('.submit');

Now, we need to set its disabled property to true.

btn.disabled = true;

We can enable it back by setting the disabled property to false.

btn.disabled = false;

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course