The following function will toggle the checkedness of all checkboxes inside a particular DOM element (for example, a <form>
element):
function toggleAllCheckBoxes(el) {
var inputs = el.getElementsByTagName("input");
var input, checked = false, i, len;
// Check whether all the checkboxes are all already checked.
// If so, we uncheck all of them. Otherwise, we check all of them.
for (i = 0, len = inputs.length; i < len; ++i) {
input = inputs[i];
if (input.type == "checkbox" && !input.checked) {
checked = true;
break;
}
}
// Now check or uncheck all of the checkboxes
for (i = 0, len = inputs.length; i < len; ++i) {
input = inputs[i];
if (input.type == "checkbox") {
input.checked = checked;
}
}
}
var form = document.getElementById("your_form_id");
toggleAllCheckBoxes(form);