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

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:







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:







<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>

Categorized in:

HTML,

Last Update: February 26, 2024