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:

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.






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:






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

Categorized in:

jQuery,

Last Update: March 13, 2024