• 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
  • VBA
  • About
You are here: Home / jQuery / Using jQuery to Disable a Button

Using jQuery to Disable a Button

January 3, 2022 Leave a Comment

We can use jQuery to disable a submit button on a form. The easiest way to do this is using the jQuery prop() method.

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

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

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

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

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

Below we will have a simple form with first name and last name 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">
  <label for="checkbox"> I agree to the terms and conditions.</label><br><br>
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

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

$("#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"><label for="checkbox"> I agree to the terms and conditions.</label><br><br>
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

<script>

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

</script>

Other Articles You'll Also Like:

  • 1.  Using jQuery to Move Element Before Another
  • 2.  Using jQuery to Get the Top Position of Element
  • 3.  Check if Input is Empty Using jQuery
  • 4.  Using jQuery to Convert a String to Lowercase
  • 5.  Using jQuery to Empty the Contents of an HTML Element
  • 6.  Using jQuery to Get the Margin of an Element
  • 7.  Using jQuery to Get the Value of a Radio Button
  • 8.  jQuery prev – Get the Previous Element of a Div
  • 9.  Get nth Child with jQuery
  • 10.  Using jQuery to Change the Text Color of a Paragraph

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy