title: how to check if a checkbox on a form is selected?
body: <p>I have a form with checkboxes. And I want to check if a specific checkbox is checked.
  I try it like this:</p>
  
  <pre><code>jQuery.validator.addMethod("checkDuplicate",function(value) {
    //var user_id = $(this).closest("form").attr('id');
          var id = $('#my_form').closest('form').attr('id');
  
          console.log(id);
          var ka = $('#ID').val();
          if ($("#contactpersonen_canseestock_2").is(':checked')  &amp;&amp; $(#my_form':checkbox:checked').length == 1) {
          console.log("checkbox checked; dont check duplicate email");
          return true;
          }
          else{
          console.log("checkbox not checked; check duplicate email");
           if ($("#contactpersonen_canseestock_2").is(":checked"))
          {
              console.log('hoi');
  }
          console.log(ka);
          return false;
          }
      }, "Email adres bestaat al");
  
      jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };
  </code></pre>
  
  <p>and this is the html:</p>
  
  <pre><code> &lt;div class="contact-label span2"&gt;
            &lt;label for="contactpersonen-email"&gt;Email adres&lt;/label&gt;
            &lt;div class="contact-input-field"&gt;
              &lt;input type="text" class="input-text span2 contactpersonen-email1 required checkDuplicate" id="contactpersonen_email1" name="contactpersonen_email1"&gt;&lt;/input&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>but every time it only goes to the else</p>
  
  <p>it is a accordian. So every time if you add a contact the id changed:</p>
  
  <pre><code>&lt;div class="contact-label span11 rights" id="formid"&gt;
            &lt;div class="row-fluid"&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canorder_0" id="contactpersonen_canorder_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canorder_0"&gt;Mag border plaatsen&lt;/label&gt;
  
               &lt;input type="checkbox" class="required checkTotal" value="Y" name="contactpersonen_canseestock_0" id="contactpersonen_canseestock_2"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseestock_0"&gt;Mag goederen afhalen&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeorders_0" id="contactpersonen_canseeorders_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseeorders_0"&gt;Mag orders inzien&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canaddaddress_0" id="contactpersonen_canaddaddress_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canaddaddress_0"&gt;Mag bezorgadressen aanpassen&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeprice_0" id="contactpersonen_canseeprice_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseeprice_0"&gt;Mag netto prijs zien&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseecredit_0" id="contactpersonen_canseecredit_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseecredit_0"&gt;Mag facturen en creditfacturen inzien&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseepickup_0" id="contactpersonen_canseepickup_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseepickup_0"&gt;Mag wijzigingen doorgeven&lt;/label&gt;
              &lt;/div&gt;
            &lt;/div&gt;        
          &lt;/div&gt;  
  </code></pre>
  
  <p>oke, I have it now like this:</p>
  
  <pre><code>jQuery.validator.addMethod("checkDuplicate",function(value) {
    //var user_id = $(this).closest("form").attr('id');
          var id = $('#my_form').closest('form').attr('id');
      var hallo = $('form').attr('id');
  console.log(hallo);
          console.log(id);
          var ka = $('#ID').val();
          if ($('#contactpersonen_canseestock_2 input:checked').val() == "1") {
          console.log("checkbox checked; dont check duplicate email");
          return true;
          }
          else{
          console.log("checkbox not checked; check duplicate email");
     //if ($("#contactpersonen_canseestock_2").is(":checked"))
  if($('#my_form:checkbox:checked').length == 1)
          {
              console.log('hoi');
  }
          console.log(ka);
          return false;
          }
      }, "Email adres bestaat al");
  
      jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };
  
  &lt;div class="contact-label span2"&gt;
        &lt;label for="contactpersonen-email"&gt;Email adres&lt;/label&gt;
        &lt;div class="contact-input-field"&gt;
          &lt;input type="text" class="input-text span2 contactpersonen_email required checkDuplicate" id="contactpersonen_email" name="contactpersonen_email"&gt;&lt;/input&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>but still only goes in the else</p>
  
  <pre><code>&lt;div class="contact-label span11 rights" id="formid"&gt;
            &lt;div class="row-fluid"&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canorder_0" id="contactpersonen_canorder_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canorder_0"&gt;Mag order plaatsen&lt;/label&gt;
  
               &lt;input type="checkbox" class="required checkTotal" value="Y" name="contactpersonen_canseestock_0" id="contactpersonen_canseestock_2"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseestock_0"&gt;Mag goederen afhalen&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeorders_0" id="contactpersonen_canseeorders_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseeorders_0"&gt;Mag orders inzien&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canaddaddress_0" id="contactpersonen_canaddaddress_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canaddaddress_0"&gt;Mag bezorgadressen aanpassen&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeprice_0" id="contactpersonen_canseeprice_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseeprice_0"&gt;Mag netto prijs zien&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseecredit_0" id="contactpersonen_canseecredit_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseecredit_0"&gt;Mag facturen en creditfacturen inzien&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseepickup_0" id="contactpersonen_canseepickup_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseepickup_0"&gt;Mag wijzigingen doorgeven&lt;/label&gt;
              &lt;/div&gt;
            &lt;/div&gt;        
          &lt;/div&gt;       
        &lt;/div&gt;
  </code></pre>
  
  <p>This is a image:
  <a href="https://i.stack.imgur.com/Q6qQ0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q6qQ0.png" alt="enter image description here"></a></p>
  
  <p>so and that is wrong. Because if only that checkbox is checked then no validation has to triggered.</p>