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>

Video Tutorial

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

Change value of href attribute of link using jQuery Check If Input Field is Empty in jQuery jQuery Select All HTML Elements Whose ID Start With Same String Count Checked Checkboxes of Specific Form with JQuery Enable and Disable Button using JQuery Add Class to HTML Tag On Click Using Jquery Change Class of HTML Tag Using JQuery Display Current Date and Time Using HTML and JQUERY Detect Change in Text Input using JQuery Refresh a Page With JQuery Execute a Function on Hover Using JQuery Open Select File Dialog Box Using JQuery Display Message When File Is Selected Using JQuery Get Class of Clicked Element Using JQuery Remove White Spaces from Start and End of String using JQuery First Digit must be 0 using JQuery OnClick Close and Open Effect using JQuery Display clicked HTML Tag Name using JQuery Delay a Function Execution using JQuery Get ID of Clicked Element using JQuery