Allen Liu
Allen Liu

Reputation: 4038

jQuery is attr update on checkbox is not triggering change event

I have a couple of checkboxes and a status box that tells you if at least 1 of the checkboxes are checked.

I also have a check All/None checkbox which works in toggling the checkboxes but it is not triggering the change event that is assigned to each of the checkboxes so the status never gets updated if the check All/None checkbox is used.

Here is my implementation:

http://jsfiddle.net/axl163/Fckvd/1/

Upvotes: 7

Views: 4835

Answers (2)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Any changes to an input element through a script WILL NOT trigger onchange event.

Alternatively you can trigger the change() while updating the changing the input like below,

$(this).prop("checked", status).change();

Full code:

function toggleChecked(status) {
    $(".chkbx").each( function() {
        $(this).prop("checked", status).change();
    });
}

Also use .prop instead of .attr

DEMO

Upvotes: 6

thecodeparadox
thecodeparadox

Reputation: 87073

function toggleChecked(status) {
    $(".chkbx").each( function() {
        $(this).prop("checked", status).change(); // you have to trigger the event
    });
}

DEMO

Upvotes: 15

Related Questions