Selected CheckBox Length in jQuery

With the help of :checked property of jquery you can find the total number of selected checkbox. It is very easy to get length of checked checkbox in jquery. In below example i have also display how you get length of unchecked checkbox.

$('.chkCountry:checked').length; // Return number of selected checkbox $('.chkCountry:not(:checked)').length; // Return number of un-checked checkbox

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('.btnGetLength').click(function () {
        alert("Checked: " + $('.chkCountry:checked').length);
        alert("UnChecked: " + $('.chkCountry:not(:checked)').length);
      });
    });
  </script>
</head>
<body>
  <div>
    <input type="checkbox" class="chkCountry" value="1" checked="checked" />India<br />
    <input type="checkbox" class="chkCountry" value="2"/>Pakistan<br />
    <input type="checkbox" class="chkCountry" value="3"/>Japan
  </div>
  <button type="button" class="btnGetLength">Get Length</button>
</body>
</html>
DISPLAY
India
Pakistan
Japan

No comments :

Please Give Your Feedback