Monset
Monset

Reputation: 657

jQuery setting checkbox state after preventing default action

I have a checkbox that needs to change state, but without invoking parent's click event. So, I created this:

$("#divTabBody@(Model.TabNumber) tr input[type='checkbox']").on('click', function(e) { 
    e.preventDefault();
    e.stopPropagation();
    if($(this).is(':checked'))
        $(this).prop('checked', false);
    else
        $(this).prop('checked', true);
});

Parent's click is not execcuting, so that's good, but the checkbox is not changing state. Why?

Upvotes: 0

Views: 28

Answers (1)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

You should just use e.stopPropagation() to stop parent's click event. Nothing else needed.

$("#divTabBody@(Model.TabNumber) tr input[type='checkbox']").on('click', function(e) { 
    e.stopPropagation();
});

Upvotes: 1

Related Questions