I'm trying to count the number of checkboxes that have been checked. But, the count remains 0 even though I click. Here is my code.
<div>
<input type="checkbox" class="category1"></input>
<input type="checkbox" class="category1"></input>
<input type="checkbox" class="category1"></input>
<input type="checkbox" class="category1"></input>
</div>
and my JS code is..
<script type="text/javascript">
$(document).ready(function(){
$(".category1").click(function(){
var category1Count = $('.category1 :checked').size();
console.log(category1Count);
});
});
</script>
There must be a simple mistake. Not able to find out. Where am I going wrong? Thanks in advance

.category1 :checkedmeans, select all selected elements that are descendants of.category1elements. Instead you want.category1:checkedwhich means "select all.category1elements which are also selected.