Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate the number of html checkbox checked using jquery

Tags:

how can i calculate the number of checkboxes that a user has checked using jquery?

what i want to do is limiting the number of checking for checkboxes in a form to 10 for example and when a user exceeds this range display a warning message.

like image 325
bogha Avatar asked Dec 24 '09 08:12

bogha


People also ask

How can I check if multiple checkboxes are checked in jQuery?

$("#my_form input[type=checkbox]"). each(function(idx, elem) { var is_checked = $(this). prop("checked"); // Do something with is_checked }); to iterate through all the checkboxes and check whether they are checked or not.


2 Answers

There are multiple methods to do that:

Method 1:

alert($('.checkbox_class_here:checked').size()); 

Method 2:

alert($('input[name=checkbox_name]').attr('checked')); 

Method: 3

alert($(":checkbox:checked").length); 
like image 65
Sarfraz Avatar answered Oct 03 '22 09:10

Sarfraz


This should work:

alert($("input:checkbox:checked").length); 
like image 27
James Wiseman Avatar answered Oct 03 '22 10:10

James Wiseman