Javascript or Jquery to check and uncheck all checkbox
<p>I have a form with a list of checkboxs. I want to create a checkAll and UncheckAll boxes for better user experience. I tried a lot of code that I got from Internet, but none of them worked. Can you help me to take a look and tell me what is the problem. thanks</p>

<pre><code>&lt;script type="text/javascript"&gt;
  function checkAll(field)
  {
  for (i = 0; i &lt; field.length; i++)
    field[i].checked = true ;
  }

  function uncheckAll(field)
  {
  for (i = 0; i &lt; field.length; i++)
    field[i].checked = false ;
  }
&lt;/script&gt;


&lt;style type="text/css"&gt;
  #user_info {
    border-collapse:collapse;
  }

  #user_info td, #user_info th {
    width:100px;
    border:1px solid #CACACA;
    padding:5px;  
  }

  #checkbox{
    padding:20px 0 20px 250px;
  }
&lt;/style&gt;

&lt;p&gt;Please choose all the users whose group_id you want to replace with that of the uploaded file&lt;/p&gt;

&lt;form id="groupImportForm" action="&lt;?php echo url_for('group_utilization/importGroupMarching') ?&gt;" method="POST"&gt;
&lt;table id="user_info"&gt;
  &lt;thead&gt;
    &lt;th&gt;User ID&lt;/th&gt;
    &lt;th&gt;Last Name&lt;/th&gt;
    &lt;th&gt;First Name&lt;/th&gt;
    &lt;th&gt;Date_Of_Birth&lt;/th&gt;
    &lt;th&gt;Old Group_ID&lt;/th&gt;
    &lt;th&gt;New Group_ID&lt;/th&gt;
    &lt;th&gt;Update GroupID&lt;/th&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;?php foreach($userGroupData as $value): ?&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;?php echo $value['user_id']; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $value['last_name']; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $value['first_name']; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $value['date_of_birth']; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $value['group_id_old']; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo !empty($value['group_id_new']) ? $value['group_id_new'] : ''; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="checkbox" name="isReplaceGroupID[&lt;?php echo $value['user_id']; ?&gt;]" value="&lt;?php echo $value['group_id_new']; ?&gt;"&gt;&lt;/input&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;?php endforeach; ?&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
  &lt;div id="checkbox"&gt;
    &lt;input type="button" name="CheckAll" value="Check All"
    onClick="checkAll(document.myform.list)"&gt;
    &lt;input type="button" name="UnCheckAll" value="Uncheck All"
    onClick="uncheckAll(document.myform.list)"&gt;

  &lt;/div&gt;
  &lt;div&gt;&lt;input type="submit" value="Continue" /&gt;&lt;/div&gt;
&lt;/form&gt;
&lt;br/&gt;
&lt;br/&gt;
</code></pre>
