Home > Web Front-end > JS Tutorial > jQuery count the number of checked checkboxes

jQuery count the number of checked checkboxes

Christopher Nolan
Release: 2025-02-27 08:53:14
Original
618 people have browsed it

Use jQuery to count the number of check boxes in the table

jQuery count the number of checked checkboxes

The following is a simple jQuery code snippet for counting the number of check boxes that have been selected in the table:

$('#table :input[type="checkbox"]:checked').length
Copy after login

jQuery statistics checkbox count FAQ

How to count the number of check boxes with pure JavaScript?

While jQuery provides an easy and efficient way to count the number of check boxes, you can also use pure JavaScript to implement this functionality. You can use the querySelectorAll method to select all check boxes and then use the Array.prototype.filter method to filter out the selected check boxes. Here is a simple example:

let checkboxes = document.querySelectorAll('input[type="checkbox"]');
let checked = Array.from(checkboxes).filter(input => input.checked);
console.log(checked.length);
Copy after login

This code will record the number of check boxes selected to the console.

How to count the number of check boxes in real time?

Yes, you can use jQuery's .change() event to count the number of check boxes in real time. This event is triggered whenever the status of the check box changes. You can use this event to update the count when the check box is selected or unchecked. Here is an example:

$('input[type="checkbox"]').change(function() {
  let count = $('input[type="checkbox"]:checked').length;
  console.log(count);
});
Copy after login

This code records the number of currently selected check boxes to the console when the check box is selected or unchecked.

How to display the number of check boxes on my page?

You can display the number of selected check boxes on a webpage by updating the text content of HTML elements. Here is an example:

$('input[type="checkbox"]').change(function() {
  let count = $('input[type="checkbox"]:checked').length;
  $('#count').text(count);
});
Copy after login

In this example, #count is the ID of the HTML element to be displayed for counting.

How to count the number of check boxes selected in a specific group?

Yes, you can use a more specific selector to count the number of check boxes selected in a specific group. For example, if your checkbox is inside a div with ID group1, you can count the number of checkboxes selected in the group like this:

$('div#group1 input[type="checkbox"]').change(function() {
  let count = $('div#group1 input[type="checkbox"]:checked').length;
  console.log(count);
});
Copy after login

This code will record the number of check boxes selected in group1 to the console.

How to count the number of unchecked check boxes?

You can use the :not(:checked) selector to count the number of unchecked check boxes. Here is an example:

let count = $('input[type="checkbox"]:not(:checked)').length;
console.log(count);
Copy after login

This code will record the number of unchecked check boxes to the console.

The above is the detailed content of jQuery count the number of checked checkboxes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template