title: How to limit the number of selected checkboxes when click on div
body: <p>I'm trying to checked the check box while click on the div.But I want to limit the number of check boxes which I can select. here is my html code.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>        $('.block').click(function () {
         $(this).find('input[type=checkbox]').prop("checked", 
      !$(this).find('input[type=checkbox]').prop("checked"));
         $(this).css('border', '3px solid black');
      });
  
  
  
      $('input[type=checkbox]').change(function(){
         if ($('input[type=checkbox]:checked').length &gt; 3) {
          $(this).prop('checked', false)
          alert("allowed only 3");
     }
  })</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
                &lt;input type="checkbox" class="cb-element" value="1" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
              &lt;input type="checkbox" class="cb-element" value="2" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;
             &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
              &lt;input type="checkbox" class="cb-element" value="3" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;
             &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
              &lt;input type="checkbox" class="cb-element" value="4" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt; 
             &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
              &lt;input type="checkbox" class="cb-element" value="5" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;
                 
             &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
              &lt;input type="checkbox" class="cb-element" value="6" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;    
             &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
             &lt;input type="checkbox" class="cb-element" value="7" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;           
             &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- end pro block --&gt;
          
          &lt;div class="col-xs-3"&gt;
            &lt;div class="block"&gt;
             &lt;input type="checkbox" class="cb-element" value="8" /&gt;
                &lt;img src="assets/img/products/1.jpg" alt=""&gt;
             &lt;/div&gt;
          &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But it is not working. Can anyone help me.This answer also I take from below question.</p>
  
  <p><a href="https://stackoverflow.com/questions/19001844/how-to-limit-the-number-of-selected-checkboxes">How to limit the number of selected checkboxes?</a></p>