title: Getting a value of checkbox into array when checked
body: <p>I am working with jQuery and this is my code so far:</p>
  
  <pre><code>$('.CategoryInput').each(function() {
      $(this).click(function() {
          var CategoryID  = $( this ).attr( "category" );
          var Clicked     = $( this ).attr( "clicked" );
          if(Clicked == 0){
              $("#Category" + CategoryID).css({backgroundColor: '#fc3b66'});
              $("#Category" + CategoryID).find( ".state" ).css({color: '#fff'});
              $(".subcat" + CategoryID).css({backgroundColor: '#fc3b66'});
              $(".subcat" + CategoryID).find( ".state" ).css({color: '#fff'});
              $(".SubCategoryInput" + CategoryID).prop('checked', true);
              $(".SubCategoryInput" + CategoryID).attr('clicked','1');
              $(this).attr('clicked','1');
          } else {                    
              $("#Category" + CategoryID).css({backgroundColor: '#fdfdfd'});
              $("#Category" + CategoryID).find( ".state" ).css({color: '#6a6d76'});
              $(".subcat" + CategoryID).css({backgroundColor: '#fdfdfd'});
              $(".subcat" + CategoryID).find( ".state" ).css({color: '#6a6d76'});
              $(".SubCategoryInput" + CategoryID).prop('checked', false);
              $(".SubCategoryInput" + CategoryID).attr('clicked','0');
              $(this).attr('clicked','0');
          }
  
          });
  
  });
  </code></pre>
  
  <p>Here is my HTML:</p>
  
  <pre><code> &lt;div class="container"&gt;                 
      &lt;h1&gt;Selecciona los productos que elaboras:&lt;/h1&gt;
      &lt;?PHP
      $r = mysql_query("SELECT * FROM `Category` WHERE `subCategory`='0' ORDER by Position ASC");
          while($rowi = mysql_fetch_array($r))
                  {
                  $id = $rowi['id'];
                  $Name = $rowi['Name'];
                  $Description = $rowi['Description'];
  
      ?&gt;
      &lt;div class="row"&gt;
          &lt;div class="maintag" id="Category&lt;?PHP echo $id;?&gt;"&gt;
              &lt;label class="state" for="categories"&gt;&lt;?PHP echo $Name;?&gt;&lt;/label&gt;
              &lt;input type="checkbox" class="CategoryInput" name="categories" category="&lt;?PHP echo $id;?&gt;" clicked="0"&gt;          
              (&lt;?PHP echo $Description;?&gt;)
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
      &lt;?PHP 
          $r1 = mysql_query("SELECT * FROM `Category` WHERE `subCategory`='$id' ORDER by Position ASC");
              while($rowi1 = mysql_fetch_array($r1))
  
              {
                  $id1 = $rowi1['id'];
                  $Name1 = $rowi1['Name'];                                
      ?&gt;
  
          &lt;div class="col-md-4" style="padding-left:0px;"&gt;
              &lt;div id="subtag&lt;?PHP echo $id1;?&gt;" class="subcat&lt;?PHP echo $id;?&gt; supersub"&gt;
                  &lt;label class="state" for="categories"&gt;&lt;?PHP echo $Name1;?&gt;&lt;/label&gt;
                  &lt;input type="checkbox" name="subcategory" class="SubCategoryInput&lt;?PHP echo $id;?&gt;" SubCategory="&lt;?PHP echo $id1;?&gt;" clicked="0"&gt;          
              &lt;/div&gt;
          &lt;/div&gt;  
  
                  &lt;?PHP } ?&gt;
          &lt;/div&gt;      
  &lt;?PHP } ?&gt;
  &lt;/div&gt;                          
  </code></pre>
  
  <p>I have several checkboxes which I use as categories. These categories have sub-categories, so this code is simply highlighting the checkboxes which are selected. If the selected checkbox is from mother category it highlights all the checkboxes for all children categories. Also setting them as <code>checked</code>.</p>
  
  <p>Everything I achieved so far as I want. Right now I am stuck on how I can get the id of the selected checkbox and add it into a variable looking like this:</p>
  
  <pre><code>var SelectedCategoryIDs = '12,435,123,124,56,34,23';
  </code></pre>
  
  <p>So when I click a mother category it will add to <code>SelectedCategoryIDs</code> the ids of all children categories. Also when I uncheck the already checked mother category it will remove all the ids from <code>SelectedCategoryIDs</code> which corresponds to all children categories.</p>
  
  <p>So can you help me achieve that?</p>
  
  <p>I hope I explained it well, if you have any questions please let me know.</p>