• 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 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 next – Get the Next Element of a Div
  • 2.  Input Mask Phone Number Using jQuery
  • 3.  jQuery val Method – Get the Value from an Input Field
  • 4.  Using jQuery to Change Background Color
  • 5.  jQuery text – Set the text of an HTML Element
  • 6.  Using jQuery to Show and Hide a Div
  • 7.  Using jQuery to Swap an Image on Hover
  • 8.  Using jQuery to Get the Position of Element
  • 9.  jQuery mouseleave – An Interactive Example of the mouseleave Method
  • 10.  How to Use jQuery to Clear a textarea Field

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