title: Adding a Select All Checkbox
body: <p>I want to add a check/uncheck all button for this script:</p>
  
  <p>php:<br>
  
  
  <pre><code>require_once('includes/commons.php');
  
  $query_get_addresses = 'SELECT * FROM user ORDER BY first_name ASC, last_name ASC';
  $result_get_addresses = mysql_query($query_get_addresses);
  $addresses_count = mysql_num_rows($result_get_addresses);
  
  ?&gt;
  </code></pre>
  
  <p>script:</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function() {
      // initialize
      var recipient = $('input[name=recipient]').val();
      var recipient_array = new Array();
      if(recipient != '') {
          recipient_array = recipient.split(',');
          for(var i = 0; i &lt; recipient_array.length; i++) {
              recipient_array[i] = $.trim(recipient_array[i]);
  
              if(recipient_array[i] == '')
                  recipient_array.splice(i, 1);
          }
  
          $('input[type=checkbox]').each(function() {
              if($.inArray(this.value, recipient_array) &gt;= 0) {
                  $(this).attr('checked', true);
              }
          });
      }
      // add and/or remove
      $('input[type=checkbox]').click(function() {
          var recipient_list = '';
  
          $('input[type=checkbox]').each(function() {
              var index = $.inArray(this.value, recipient_array);
              if(this.checked) {
                  if(index &lt; 0)
                      recipient_array.push(this.value);
              }
              else {
                  if(index &gt;= 0) {
                      recipient_array.splice(index, 1);
                  }
              }
          });
  
          $('input[name=recipient]').val(recipient_array);
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>html:</p>
  
  <pre><code>&lt;h2&gt;Choose recipient&lt;/h2&gt;
  &lt;div&gt;&lt;?php
  
  if($addresses_count &gt; 0) {
  
      ?&gt;&lt;form name="select-recipient-form"&gt;&lt;ul&gt;&lt;?php
  
          while($row_get_addresses = mysql_fetch_assoc($result_get_addresses)) {
              $row_get_addresses = unsanitize($row_get_addresses);
  
              ?&gt;&lt;li&gt;&lt;input type="checkbox" name="recipient[]" id="recipient-1" value="&lt;?php echo $row_get_addresses['recipient']; ?&gt;" /&gt;&lt;label for="recipient-1"&gt;&lt;?php echo $row_get_addresses['first_name'].' '.$row_get_addresses['last_name'].' &amp;lt;'.$row_get_addresses['recipient'].'&amp;gt;'; ?&gt;&lt;/label&gt;&lt;/li&gt;&lt;?php
          }
  
      ?&gt;&lt;/ul&gt;
      &lt;/form&gt;&lt;br /&gt;&lt;?php
  
  } else {
  
      ?&gt;&lt;p&gt;You don't have any contacts.&lt;/p&gt;&lt;?php
  }
  
  ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have tried to follow some guides. Like <a href="http://www.plus2net.com/javascript_tutorial/checkbox-checkall.php" rel="nofollow">this one</a> for example but I can't get it to work.
  Any idea how I can achieve this?
  Thanks</p>