Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of checkboxes selected without reloading the page?

I'm expanding further on this question.

I currently have an asp.net hyperlink for Select All (NumberOfCheckBoxes) which works, but what I'm looking to do is add another link, possibly just a standard HTML hyperlink for Uncheck Selected (NumberOfCheckedBoxes) and update the value of NumberOfCheckedBoxes as checkboxes are ticked, without reloading the page.

I have javascript already for unchecking them, but not counting them and printing it to screen.

I'm not sure if JQuery is the way to go with this or just standard Javascript.

Thanks in advance for any help.

like image 979
LiamGu Avatar asked Dec 19 '25 02:12

LiamGu


2 Answers

With jQuery you can:

$("input:checkbox:checked").length;
like image 77
Zed Avatar answered Dec 20 '25 14:12

Zed


Try this:

var inputElems = document.getElementsByTagName("input"),
    count = 0;
for (var i=0; i<inputElems.length; i++) {
    if (inputElems[i].type === "checkbox" && inputElems[i].checked === true) {
        count++;
    }
}
like image 20
Gumbo Avatar answered Dec 20 '25 14:12

Gumbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!