• 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 / HTML / Making a Checkbox Disabled in HTML

Making a Checkbox Disabled in HTML

January 23, 2022 Leave a Comment

We can make a checkbox disabled in HTML using the disabled attribute. We assign the disabled attribute to an input to disable that input.

<input type="text" disabled>

We can also use the jQuery prop() method to change the disabled attribute of an input to true.

$('input').prop('disabled', true);

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery('input').prop('disabled', true);

An example of using jQuery 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. Usually you have to click the link to enable the checkbox to be checked.

So below we will have a form that only lets you submit the information if you agree to the terms/conditions, by clicking the link then 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" disabled><label for="checkbox"> I agree to the <a id="fake-terms" href="#">terms and conditions</a>.</label><br><br>
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

We will then use jQuery to first enable the checkbox when clicked, and then disable or enable the submit button based on whether the checkbox is checked or not. Here is the code:

$("#fake-terms").click(function(){
  $('#checkbox').prop('disabled', false);
});

$("#checkbox").click(function(){
  if( $('#checkbox').is(':checked') ){
    $('button').prop('disabled', false);
  } else {
    $('button').prop('disabled', true);
  } 
});

The final code and output for this example of how to enable and disable a submit button using jQuery 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" disabled><label for="checkbox"> I agree to the <a id="fake-terms" href="#">terms and conditions</a>.</label><br><br>
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

<script>

$("#fake-terms").click(function(){
  $('#checkbox').prop('disabled', false);
});

$("#checkbox").click(function(){
  if( $('#checkbox').is(':checked') ){
    $('button').prop('disabled', false);
  } else {
    $('button').prop('disabled', true);
  } 
});

</script>

Other Articles You'll Also Like:

  • 1.  What is the Correct HTML for Adding a Background Color to a Div?
  • 2.  What is the Correct HTML for Making a Text Area?
  • 3.  What is the Correct HTML for Making a Drop-Down List?
  • 4.  How Do You Select All p Elements Inside a Div Element?
  • 5.  What is the Correct HTML for Inserting a Background Image?
  • 6.  How to Call a JavaScript Function From HTML
  • 7.  Clicking on an Image to Enlarge it in HTML
  • 8.  HTML subscript – How to Lower Text in HTML
  • 9.  HTML span width – Set the Width of a Span in HTML
  • 10.  HTML superscript – How to Raise Text in HTML

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