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

How to Set All Checkbox to Checked in jQuery

March 29, 2022 Leave a Comment

To set all checkboxes to checked in jQuery, the simplest way is to use the jQuery prop() method along with the checked property.

$("input[type='checkbox']").prop("checked", true);

Let’s say I have the following HTML form:

<form>
  <input type="checkbox" id="pasta" name="pasta" value="pasta">
  <label for="pasta"> I like pasta</label>
  <input type="checkbox" id="seafood" name="seafood" value="seafood">
  <label for="seafood"> I like seafood</label>
  <input type="checkbox" id="hamburgers" name="hamburgers" value="hamburgers">
  <label for="hamburgers"> I like hamburgers</label>
  <input type="submit" value="Submit">
</form>

To start, all of the checkboxes in the form above will be unchecked. We could check them all individually if we wanted to, but we can also use some jQuery code to do this in one step.

To do this, we can use the prop() method as we showed above to set all checkboxes to checked.

$("input[type='checkbox']").prop("checked", true);

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

jQuery("input[type='checkbox']").prop("checked", true);

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

$("input[type='checkbox']").prop("checked", false);

Set All Checkbox to Checked in jQuery Using a Click

We can check all checkboxes in a form using jQuery very easily by combining the prop() method with a click event.

In our HTML, we will have a simple form with 3 items, and a Select All checkbox that will allow us to set all checkboxes to checked.

<form>
  <input type="checkbox" id="selectAll" name="selectAll" value="selectAll">
  <label for="selectAll"> Select All</label><br>
  <input type="checkbox" id="pasta" name="pasta" value="pasta">
  <label for="pasta"> I like pasta</label><br>
  <input type="checkbox" id="seafood" name="seafood" value="seafood">
  <label for="seafood"> I like seafood</label><br>
  <input type="checkbox" id="hamburgers" name="hamburgers" value="hamburgers">
  <label for="hamburgers"> I like hamburgers</label><br><br>
  <input type="submit" value="Submit">
</form>

We can utilize both the jQuery click() method

and jQuery prop() method to set the checked property of the checkboxes to true.

We will also add some code to uncheck all checkboxes as well, by just changing the checked property to false. To know whether to check or uncheck all checkboxes, we will have see if the selectAll checkbox is checked.

$('#selectAll').click(function(){
  if( $("#selectAll").is(':checked') ){
    $("input[type='checkbox']").prop("checked", true);
  } else {
    $("input[type='checkbox']").prop("checked", false);
  }
});

The final code and output for this example of how to set all checkbox to checked using jQuery is below:

Code Output:





Full Code:

<form>
  <input type="checkbox" id="selectAll" name="selectAll" value="selectAll">
  <label for="selectAll"> Select All</label><br>
  <input type="checkbox" id="pasta" name="pasta" value="pasta">
  <label for="pasta"> I like pasta</label><br>
  <input type="checkbox" id="seafood" name="seafood" value="seafood">
  <label for="seafood"> I like seafood</label><br>
  <input type="checkbox" id="hamburgers" name="hamburgers" value="hamburgers">
  <label for="hamburgers"> I like hamburgers</label><br><br>
  <input type="submit" value="Submit">
</form>

<script>

$('#selectAll').click(function(){
  if( $("#selectAll").is(':checked') ){
    $("input[type='checkbox']").prop("checked", true);
  } else {
    $("input[type='checkbox']").prop("checked", false);
  }
});

</script>

Hopefully this article has been useful for you to understand how to set all checkboxes to checked using jQuery.

Other Articles You'll Also Like:

  • 1.  jQuery append – Insert Element at End of Another Element
  • 2.  jQuery hover – An Interactive Example of the hover Method
  • 3.  jQuery on change
  • 4.  jQuery Compare Dates
  • 5.  How to Set Checkbox Checked With jQuery
  • 6.  Examples of Text Animations Using jQuery
  • 7.  Using jQuery to Append Text to textarea Field
  • 8.  Using jQuery to Replace HTML Inside Div
  • 9.  Using jQuery to Add CSS Important Style
  • 10.  jQuery first() – Get the First Element

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