4

I have the 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 a user should be able to click the div.item and/or the checkbox inside it and it should check or uncheck the checkbox and add or remove the selected class from the div.

It works when clicking the div, but clicking the checkbox instantly unchecks it again...

2
  • what do you want the checkbox to do? Commented Jul 1, 2013 at 10:12
  • 2
    see this jsfiddle.net/sX3w9/11 Commented Jul 1, 2013 at 10:13

7 Answers 7

8

Try the 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 is that when you check checkbox clicking on div occurs and it makes checbox unchecked again.

Sign up to request clarification or add additional context in comments.

1 Comment

A very good solution but it worked with .prop() for me, since after the first click the checkbox did not get the 'checked' property.
4

All you need to do is check the event target of the click, and if it is a checkbox, don't do the manual changing of the checked property:

$(function () {
    $(".item").on("click", function (e) {
        if (!$(e.target).is(':checkbox')) {
            var $checkbox = $(this).find("input[type='checkbox']");
            $checkbox.click();
        }
        $(this).toggleClass('selected');
    });
}

Note: you can use $checkbox.prop("checked", !objCheckbox.prop("checked")); to toggle the checkbox, but if you have events that only trigger onclick of the checkbox, $checkbox.click(); works best.

Comments

0

that is because since you checkbox is inside the div...it is taking the click event of its parent div.. eventBubble(). check if the clicked element is the target element.. if yes do the thing you want...

try this

$('.item').on('click', function(e) {
    if(e.target==this){
      var checkbox = $(this).find('input');
      checkbox.prop("checked", !checkbox.prop("checked"));  
      $(this).toggleClass('selected');
    }else{
       $(this).toggleClass('selected');
    }
});

fiddle here

1 Comment

I don't think that is the proper solution for what the OP wants. If you click the checkbox now, the class selected is not toggled.
0

This would work well with a label:

<label for="checkboxid">
    <div class="item">
        <input type="checkbox" id="checkboxid" />
    </div>
</label>

$('#checkboxid').on('change', function() {
    $('.item').toggleClass('selected');
});

Comments

0

Your code works with div click, but clicking the checkbox instantly unchecked because of that, you write a code !checkbox.attr("checked") to unchecks check-box.

So, if you want to do this work - first you need to revert the Checked attribute.

Try This Example

There is an another condition, every time click on check-box binds click event to div. So if you don't want this you need to check clicked Element. For this you use event.toElement.

Try This Example

Comments

0

This is what I used:

$('body').on('click', '.item', function (event) {
    if (event.target.tagName.toLowerCase() == 'input') return;
    var objCheckbox = $(this).find("input[type=checkbox]");
    if (objCheckbox.length >= 1) {
        objCheckbox.prop("checked", !objCheckbox.prop("checked"));
    }
});
$('body').on('click', 'input[type=checkbox]', function (e) {
    $(this).parent().click();
    $(this).prop("checked", !$(this).prop("checked"));
});

Don't bind to body if you have other checkboxes on your page.

Comments

-2

The event is fired twice when you click the check box. You need to stop the event being bound on the checkbox and bind the event on the parent element only like so.

http://jsfiddle.net/sX3w9/15/

$('.item').on('click', function() {
var checkbox = $(this).find('input');

checkbox.click(function(e){
    e.stopPropagation();
}).attr("checked", !checkbox.attr("checked"));

$(this).toggleClass('selected');

});

1 Comment

every time click on .item it binds click event to input checkbox child

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.