jquery (EDITED to toggle check/uncheck all):
$(document).ready(function() {
$("#toggleAll").click(function() {
if ($(this).attr("checked")) {
$("#chex :checkbox").attr("checked", true);
} else {
$("#chex :checkbox").attr("checked", false);
}
});
});
The reason why I had to do a click()
then an if
statement to check for checked
status is because if you try to "toggle
" a checkbox, the checkbox being toggled will not retain its checked status. This way it retains the check status and effectively toggles.
HTML:
<input type="checkbox" id="toggleAll" />
<div id="chex">
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</div>