I’ve got a series of checkboxes set up as an array (name=”action[]”) and a JavaScript function linked to another checkbox to check/uncheck all of them. It works fine when there are at least two checkboxes to be checked, but if there’s only one it does nothing. Can anyone point me in the right direction? Thanks!
HTML:
<input type="checkbox" name="checkall" id="checkall" class="box" onClick="doCheck(this, 'action');">
<input type="checkbox" name="action[]" id="action" value="' . $something['someid'] . '" class="box">
<input type="checkbox" name="action[]" id="action" value="' . $something['someid'] . '" class="box">
JavaScript:
function doCheck(mainChk, fields) {
for(i = 0; i < mainChk.form.elements[fields].length; i++) {
mainChk.form.elements[fields][i].checked = mainChk.checked;
}
}
if you would like to use jquery, you could you this code: