Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing checkbox from being deselected when click trigger is on parent element

Kind of a funny problem here.

I want to check the checkbox when the parent div is checked, but it is DESELECTING the checkbox when the CHECKBOX is clicked on ;)

It works fine when the name is clicked on. How do i solve this?

//layout
<div class="parent">
   <input type="checkbox"/> <a>Name</a>
</div>

//check the box when parent div is clicked
$(".parent").click(function(event){
   event.preventDefault();
   checkbox=$(this).find("input:checkbox");
   checkbox.attr("checked","checked");
});
like image 771
meow Avatar asked Feb 21 '11 02:02

meow


1 Answers

Try this.

<div class="parent">
   <input type="checkbox" class="check"/> <a>Name</a>
</div>

$(".parent").click(function(event){
    $(".check:not(:checked)").attr('checked','checked');
});
$(".check").click(function(event){
   event.stopPropagation();
});

jsFiddle is here

like image 64
naveen Avatar answered Sep 28 '22 01:09

naveen