Sometimes, we want to select all checkboxes using jQuery
In this article, we’ll look at how to select all checkboxes using jQuery.
Select All Checkboxes Using jQuery
To select all checkboxes using jQuery, we can select all the checkbox input elements.
Then we call prop
to set the checked
attribute of each checkbox to a given value.
For instance, we write:
<p id="checkBoxes">
<input type="checkbox" class="checkBoxClass" id="Checkbox1" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox2" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox3" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox4" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox5" />
<br />
</p>
to add the checkboxes.
Then we write:
$(".checkBoxClass").prop('checked', true);
to call the prop
method on all the checkboxes with class checkBoxClass
and set their checked
attributes to true
.
Therefore, all the checkboxes will be checked.
Conclusion
To select all checkboxes using jQuery, we can select all the checkbox input elements.
Then we call prop
to set the checked
attribute of each checkbox to a given value.