get Count of Checked Checkboxes
HTML / jQuery

How to get Count of Checked Checkboxes using jQuery

get Count of Checked  Checkboxes

Hi, Are you looking for the answer of how to get count of checked checkboxes then you should check this solution. This post will give you the proper knowledge with step by step instructions so that you can do it easily in future whenever you need it.

We will use the .length property of JavaScript to get the count of checked checkboxes. Basically .length property returns the number of characters in a string. So this property can help us to get this task done in a proper way.

Here is the method to do this task with .length property of JavaScript.

First we will create a variable with the name count you can give it any name as per your choice. This variable should contain all the checked checkboxes. So after creating the count variable we will write the following code to get it done $("input[type='checkbox']:checked");. See the example below.

var count = $("input[type='checkbox']:checked");

Note: The :checked is a pseudo-class selector in CSS which will help us to select only checked checkboxes. Then we can count it easily using JavaScript.

Now the count variable is containing all checked checkboxes. We only need to count them. We will use .length property with count variable to count them like the given example.

Make sure to include jQuery before adding the given code.

var count = $("input[type='checkbox']:checked");
alert(count.length);

Hope this helps. Thanks !

Author

I am a Full Stack Web Developer. I have more than 8 years of experience in the web design and development industry.

Leave a Reply

Your email address will not be published. Required fields are marked *