javascript - Toggle Checkbox with jQuery on parent click -


i have following code example: http://jsfiddle.net/sx3w9/

$('.item').on('click', function() {             var checkbox = $(this).find('input');             checkbox.attr("checked", !checkbox.attr("checked"));                 $(this).toggleclass('selected');         }); 

where user should able click div.item and/or checkbox inside , should check or uncheck checkbox , add or remove selected class div.

it works when clicking div, clicking checkbox instantly unchecks again...

try following code:

$('.item').on('click', function() {             var checkbox = $(this).find('input');             checkbox.attr("checked", !checkbox.attr("checked"));                 $(this).toggleclass('selected');         }); $(".item input").on("click", function(e){     e.stoppropagation();     $(this).parents(".item").toggleclass('selected'); }); 

the problem when check checkbox clicking on div occurs , makes checbox unchecked again.


Comments

Popular posts from this blog

php - Passing multiple values in a url using checkbox -

compilation - PHP install fails on Ubuntu 14 (make: *** [sapi/cli/php] Error 1) PHP 5.6.20 -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -