• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Disable a Button

Using JavaScript to Disable a Button

January 4, 2022 Leave a Comment

We can use JavaScript to disable a submit button on a form. The easiest way to do this is by using the disabled property.

document.getElementById("button1").disabled = true;

Here we can use the disabled property of the button input, and change its disabled property to true.

An example of using JavaScript to enable/disable a button on a form

Below we will have a simple form with name and email fields and a submit button. One thing that is common in a lot of online forms is having a checkbox that a user must click on to agree to certain terms/conditions. So below we will have a form that only lets you submit the information if you agree to the terms/conditions, by checking the checkbox. Here is the HTML code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="checkbox" id="checkbox" name="checkbox" value="Checkbox" onclick="checkClick()">
  <label for="checkbox"> I agree to the terms and conditions.</label><br><br>
  <button id="button1" type="submit" value="Submit" disabled>Submit</button>
</form>

We will then use Javascript to disable or enable the submit button based on whether the checkbox is checked or not. We attach an onclick event to the checkbox that will run the checkClick() function below. This can also be done using jQuery. Here is the code to do it using JavaScript:

function checkClick(){
  if( document.getElementById("checkbox").checked ){
    document.getElementById("button1").disabled = false;
  } else {
    document.getElementById("button1").disabled = true;
  } 
};

The final code and output for this example of how to enable and disable a submit button using JavaScript is below:

Code Output:




Full Code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="checkbox" id="checkbox" name="checkbox" value="Checkbox" onclick="checkClick()"><label for="checkbox"> I agree to the terms and conditions.</label><br><br>
  <button id="button1" type="submit" value="Submit" disabled>Submit</button>
</form>

<script>

function checkClick(){
  if( document.getElementById("checkbox").checked ){
    document.getElementById("button1").disabled = false;
  } else {
    document.getElementById("button1").disabled = true;
  } 
}

</script>

Other Articles You'll Also Like:

  • 1.  How to Convert a String to Lowercase In JavaScript
  • 2.  Using JavaScript to Scroll to Bottom of Div
  • 3.  JavaScript offsetTop – Get the Top Position of Element
  • 4.  Set Text with the textContent Property in JavaScript
  • 5.  Check if Character is Uppercase in JavaScript
  • 6.  Using JavaScript to Remove the First Character From a String
  • 7.  innerHTML vs outerHTML
  • 8.  Using JavaScript to Add to Array
  • 9.  How to Change an Image on Hover Using JavaScript
  • 10.  Subtract All Numbers in an Array Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy