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");

http://jsfiddle.net/seanwessell/xsazxevk/


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -