How can I implement a "Select All" checkbox in my ASP.NET MVC app?
asp.net-mvc, checkbox, jquery
Solution
This will keep all the individual checkboxes the same as the "check all" one
$("#id-of-checkall-checkbox").click(function() {
$(".class-on-my-checkboxes").attr('checked', this.checked);
});
This will keep the "check all" one in sync with whether or not the individual checkboxes are actually all checked or not
$(".class-on-my-checkboxes").click(function() {
if (!this.checked) {
$("#id-of-checkall-checkbox").attr('checked', false);
}
else if ($(".class-on-my-checkboxes").length == $(".class-on-my-checkboxes:checked").length) {
$("#id-of-checkall-checkbox").attr('checked', true);
}
});
Problem
I have a table with a column full of checkboxes. At the top I would like to have a single "Select All" checkbox that would check all the checkboxes on that page. How should I implement this? I'm using jQuery as my JavaScript framework if it matters.