Count Checked Checkboxes of Specific Form with JQuery

In this tutorial we will see how to Count Checked Checkboxes of Specific Form with JQuery. The jQuery :checked Selector, .filter() method and .length property are used for this purpose.

HTML Code

HTML Code is given below, This code contains three checkbox type input elements with three labels. HTML button is also there and click event is tied with this button using jQuery. On clicking this button a function will be executed which will count the number of checked checkboxes.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Count Checked Checkboxes of Specific Form with JQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>    
</head>
<body>
<form id="main">
 <label>HTML</label>
 <input type="checkbox" class='checkbox'>
 <label>CSS</label>
 <input type="checkbox" class='checkbox'>
 <label>JAVASCRIPT</label>
 <input type="checkbox" class='checkbox'>
</form>
<button>Count Checkboxes</button>
</body>
</html>

jQuery :checked Selector

The jQuery :checked Selector is used for checkboxes, select tag and radio buttons. In this example the :checked Selector is used for those checkboxes which are checked. All other checkboxes will be ignored.

jQuery filter() method

The jQuery filter() method return only those HTML elements that match the selector or pass the function test.

In this example jQuery filter() method will return only those checkboxes that are checked because we used :checked Selector in our filter method.

length Property

The length Property returns the number of elements in the jQuery object.

In this example the length Property will count the number of checked checkboxes.

JQuery Code

JQuery Code is given below, The code will only be executed once the document is ready. Once document is ready then the form with id 'main' is selected. Then checkboxes of this form are selected.

On button click using the click method of jQuery, a function is executed which will count the number of checked checkboxes. This number is then displayed using alert method.

The good thing about this code is that this will only count checked checkboxes of a specific html form (with id 'main' in this example) on current page, not all forms.

Take a look at the code given below.

<script>
$(document).ready(function(){
  var form = $("#main");
  var checkboxes = $("input:checkbox", form);
  $("button").click(function(){
  var x =checkboxes.filter(':checked').length;
  alert(x);
  });
});
</script>

Demo

Video Tutorial

Watch video tutorial on how to Count Checked Checkboxes of Specific Form with JQuery.

How To Fade in Image with jQuery How To Change image src with jQuery How To Display Message on Mouse Enter and Leave Events How To Scroll Page to the Bottom using jQuery How To Scroll Page to the Top using jQuery Check radio button Dynamically with jQuery Change Background Color on hover with jQuery Hide and Show div using jQuery Get value of input element with jQuery Remove last child of parent element with jQuery Add li tag to the end of list jQuery Delete first row of Table with jQuery Delete last row of Table with jQuery Add first list item using jQuery jQuery Get id of Button jQuery toggle show hide on click Disable Button after Click with jQuery Get Button Text with jQuery jQuery Dynamic li Click Event How To Remove all Spaces in String with jQuery Get Selected file name in jQuery Remove Last Option of Select tag with jQuery Remove First Option from Select tag in jQuery jQuery Capitalize First Letter of String jQuery Check input value length