I have created some fake checkboxes using elements like <span>
after the real checkbox and they are working fine! However, I am having trouble counting the checked checkboxes when a checkbox is clicked.
I have tried using methods like:
$(".elm:checked").length;
but it doesn't seem to work properly!
Below is an example of my code for counting checked checkboxes:
$(document).ready(function(){
$(".mycheckbox").click(function(){
$(".checkbox").click(function(){
alert($(".checkbox:checked").length); // This returns 0 when a checkbox is checked and 1 when unchecked, which is incorrect. It should return 1 when checked and 0 when unchecked.
});
});
});
I believe this issue is caused by my fake checkboxes. When a user clicks on the fake checkbox, jQuery triggers a click event on the real checkbox, and then it works correctly!
Does anyone have any ideas or suggestions?
[Sorry, English is not my first language]
jsfiddle: http://jsfiddle.net/E2kjM/2/