I have a form with checkboxes with the input being stored in an array:
<input type="checkbox" name="lineup[]" value="1">Tom</input>
<input type="checkbox" name="lineup[]" value="2">David</input>
<input type="checkbox" name="lineup[]" value="3">Sarah</input>
Using jQuery I want to find how many checkboxes are ticked/checked:
var total=$(this).find('input[name=lineup]').serialize();
alert(total.length);
However the total is always output a 0. What am I doing wrong?
So user can select as many checkboxes they want but sum can't exceed 10.
Use :checked
in the selector, and don't serialize it, just get the length.
var total=$(this).find('input[name="lineup[]"]:checked').length;
Also use []
in the selector, because your checkboxes use []
in the name. As @Felix Kling points out, it is part of the name and so you have to explicitly specify the []
.
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With