Javascript check/uncheck all checkbox function only works when there is more than one checkbox
<p>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!</p>

<p>HTML:</p>

<pre><code>&lt;input type="checkbox" name="checkall" id="checkall" class="box" onClick="doCheck(this, 'action');"&gt;

&lt;input type="checkbox" name="action[]" id="action" value="' . $something['someid'] . '" class="box"&gt;
&lt;input type="checkbox" name="action[]" id="action" value="' . $something['someid'] . '" class="box"&gt;
</code></pre>

<p>JavaScript:</p>

<pre><code>function doCheck(mainChk, fields) {
  for(i = 0; i &lt; mainChk.form.elements[fields].length; i++) {
    mainChk.form.elements[fields][i].checked = mainChk.checked;
  }
}
</code></pre>
