Skip to content

Instantly share code, notes, and snippets.

@23maverick23
Last active October 12, 2015 03:58
Show Gist options
  • Save 23maverick23/3967531 to your computer and use it in GitHub Desktop.
Save 23maverick23/3967531 to your computer and use it in GitHub Desktop.
JavaScript: Check all checkboxes in table rows
/*
* Check all table rows when header checkbox is checked.
* Also add a class to the table row for highlighting.
*/
// Set initial header checkbox state to false
var checkallEnabled = false;
// When checkbox in table header is clicked...
$("#check_all").click(function() {
// Toggle the state of all checkboxes
$(":checkbox").attr("checked", !checkallEnabled);
// OPTIONAL: Set the table row class for checked rows
if ($(":checkbox").attr("checked")) {
$("tbody>tr").addClass("highlight");
}
else {
$("tbody>tr").removeClass("highlight");
}
// Set the header checkbox state after each click
checkallEnabled = !checkallEnabled;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment