javascript - Count how many times a single checkbox is checked -
how can count how many times checkbox toggled unchecked checked?
<input type="checkbox" id="auto_<?= $sub_module_id ?>" onclick="test(<?= $sub_module_id ?>)" />
my javascript code:
function test(sub_module_id){ $('#auto_'+sub_module_id).is(':checked'); //returns true, // want count when checked }
instead of having variable assigned count of how many times checked off why don't assign count data property since count in relation element?
$(":checkbox").click(function () { var $this = $(this); if (typeof ($this.data("count")) == "undefined") { $this.data("count", 0); }; if ($this.is(":checked")) { $this.data("count", $this.data("count") + 1) alert($this.data("count")); } });
then when need access can use $("#check1").data("count");
Comments
Post a Comment