counting checkbox and showing the value
I am using the following jquery to show the selected number of checkboxes
selected
$(document).ready(function(){
$('.check:button').toggle(function(){
$('input:checkbox').attr('checked','checked');
var count = $("input[type=checkbox]:checked").size();
$(this).val('uncheck all')
$("#count").text(count+ " item(s) selected");
},function(){
$('input:checkbox').removeAttr('checked');
var count = $("input[type=checkbox]:checked").size();
$(this).val('check all');
$("#count").text(count+ " item(s) selected");
})
})
here is the html
<input type="button" class="check" value="check all" />
<input type="checkbox" class="cb-element" /> Checkbox 1
<input type="checkbox" class="cb-element" /> Checkbox 2
<input type="checkbox" class="cb-element" /> Checkbox 3
<p id="count"></p>
When i check all it shows the right numbers, like 3 items selected and
when i uncheck all, then it shows o items selected.
But when I remove individual selected checkbox then the count does not
show up. like if i remove one, then the count will still be 3 and not the
2.
how can i achieve this?
No comments:
Post a Comment