• 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 / jQuery / How to Set Checkbox Checked With jQuery

How to Set Checkbox Checked With jQuery

December 2, 2021 Leave a Comment

To set a checkbox as checked with jQuery, the simplest way is to use the jQuery prop() method and to use the ‘checked’ property.

$('#checkbox').prop('checked', true);

If we want to uncheck a checkbox using jQuery, we can use the prop() method and set it to false:

$('#checkbox').prop('checked', false);

Let’s say I have the following HTML:

<div id="div">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox 1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-2" value="Checkbox 2" checked>
</div>

To set the first checkbox as checked, we can do the following:

$('#checkbox-1').prop('checked', true);

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

jQuery('#checkbox-1').prop('checked', true);

How to Set a Checkbox as Checked on Click with jQuery

We can check a checkbox using jQuery very easily by combining the prop() method with a click event.

Let’s say we have the following HTML code and we want to set the checked value of our checkbox as checked in the HTML below:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox">
  <label for="checkbox-set-1">Checkbox </label>
  <div id="click-me">Check Checkbox</div>
</div>

We can utilize both the jQuery click() method and jQuery prop() method to set the checked property of the checkbox.

Below is the Javascript code which will allow us to set our checkbox as checked.

$('#click-me').click(function(){
  $('#checkbox-1').prop('checked', true);
});

The final code and output for this example of how to check a checkbox on click using jQuery and JavaScript is below:

Code Output:

Check Checkbox

Full Code:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox">
  <label for="checkbox-set-1">Checkbox </label>
  <div id="click-me">Check Checkbox</div>
</div>

<script>

$('#click-me').click(function(){
  $('#checkbox-1').prop('checked', true);
});

</script>

How to Uncheck a Checkbox Using jQuery

Just as easily as we can check a checkbox, we can also uncheck a checkbox using jQuery.

Let’s say we have the following HTML code and we want to set the checked value of our checkbox as unchecked in the HTML below:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1-1" value="Checkbox" checked>
  <label for="checkbox-set-1"> Checkbox</label>
  <div id="click-me-1">Uncheck Checkbox</div>
</div>

We can once again utilize both the jQuery click() method and jQuery prop() method to set the checked property of the checkbox as false.

Below is the JavaScript code which will allow us to set our checkbox as unchecked.

$('#click-me').click(function(){
  $('#checkbox-1').prop('checked', false);
}); 

The final code and output for this example of how to uncheck a checkbox on click using jQuery and Javascript is below:

Code Output:

Uncheck Checkbox

Full Code:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1-1" value="Checkbox" checked>
  <label for="checkbox-set-1"> Checkbox</label>
  <div id="click-me-1">Uncheck Checkbox</div>
</div>

<script>

$('#click-me-1').click(function(){
  $('#checkbox-1').prop('checked', false);
});

</script>

Hopefully this article has been useful for you to understand how to check a checkbox and uncheck a checkbox using jQuery.

Other Articles You'll Also Like:

  • 1.  jQuery get first child
  • 2.  Using jQuery to Add Class to HTML Element
  • 3.  Get the Window Scroll Position Using jQuery
  • 4.  jQuery Compare Dates
  • 5.  Using jQuery to Add Option to Select List
  • 6.  Using jQuery to Remove Class from an HTML Element
  • 7.  Get the Height of a Div Using jQuery
  • 8.  jQuery before – Insert HTML Before Another Element
  • 9.  Using jQuery to Hide Element by Class
  • 10.  Add Style to HTML Element Using jQuery

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