title: Twitter Bootstrap Checkbox JavaScript
body: <p>I am making checkboxes/radio/toggle groups for Bootstrap, but I failed with checkboxes - can you help me to make them work?</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;form id="edit-accounts" class="form-horizontal" method="post"&gt;
      &lt;fieldset&gt;
  
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Radio Group:&lt;/label&gt;
              &lt;div class="controls btn-group" data-toggle="buttons-radio" data-toggle-name="radio_group"&gt;
                  &lt;button type="button" value="1" class="btn" data-toggle="button"&gt;first&lt;/button&gt;
                  &lt;button type="button" value="2" class="btn" data-toggle="button"&gt;second&lt;/button&gt;
                  &lt;button type="button" value="3" class="btn" data-toggle="button"&gt;third&lt;/button&gt;
              &lt;/div&gt;
              &lt;input type="hidden" name="radio_group" value="1" /&gt;
          &lt;/div&gt;
  
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Checkbox Group:&lt;/label&gt;
              &lt;div class="controls btn-group" data-toggle="buttons-checkbox" data-toggle-name="checkbox_group"&gt;
                  &lt;button type="button" value="1" class="btn" data-toggle="button"&gt;js&lt;/button&gt;
                  &lt;button type="button" value="2" class="btn" data-toggle="button"&gt;fiddle&lt;/button&gt;
                  &lt;button type="button" value="3" class="btn" data-toggle="button"&gt;alpha&lt;/button&gt;
              &lt;/div&gt;
              &lt;input type="hidden" name="checkbox_group" value="1" /&gt;
          &lt;/div&gt;
  
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Toggle:&lt;/label&gt;
              &lt;div class="controls btn-group" data-toggle="button" data-toggle-name="button_group"&gt;
                  &lt;button type="button" value="1" class="btn" data-toggle="button"&gt;on/off&lt;/button&gt;
              &lt;/div&gt;
              &lt;input type="hidden" name="button_group" value="1" /&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;​
  </code></pre>
  
  <p>JavaScript:</p>
  
  <pre><code>$(function () {
      $('div.btn-group[data-toggle-name=*]').each(function () {
          var form = $(this).parents('form').eq(0);
          var name = $(this).attr('data-toggle-name');
          var hidden = $('input[name="' + name + '"]', form);
          var type = $(this).attr('data-toggle');
          var check = [];
          $('button', $(this)).each(function () {
              $(this).live('click', function () {
                  if($(this).val() == hidden.val()) {
                      switch(type) {
                      case 'button':
                          $(this).removeClass('active');
                          hidden.val(0);
                          break;
                      case 'buttons-checkbox':
                          $(this).removeClass('active');
                          break;
                      default:
                          hidden.val($(this).val());
                      }
                  } else {
                      switch(type) {
                      case 'button':
                          $(this).addClass('active');
                          hidden.val(1);
                          break;
                      case 'buttons-checkbox':
                          $(this).addClass('active');
                          break;
                      default:
                          hidden.val($(this).val());
                      }
                  }
              });
              if($(this).val() == hidden.val()) $(this).addClass('active');
          });
      });
  });​
  </code></pre>
  
  <p><a href="http://jsfiddle.net/tkXBg/" rel="nofollow" title="look on working prototype">working prototype</a></p>