Cindy93
Cindy93

Reputation: 1300

Jquery Check all checkbox

Why does my check all button work once and doesn't work at 3rd click. the check all button only works at firts click. I check the dom and its updating but the view does. What is the cause of the problem?

FIDDLE

jQuery('.sp_country').click(function () {
    var checkbox = $(this).find(":checkbox"),
        checked = checkbox.is(":checked");
    checkbox.prop("checked", !checked);

});

jQuery('.sp_country input:checkbox').on('click', function (e) {
    e.stopImmediatePropagation();
    var checked = (e.currentTarget.checked) ? false : true;
    e.currentTarget.checked = (checked) ? false : checked.toString();
});

jQuery('#bt_checkbox').click(function (e) {
    e.stopImmediatePropagation();
    if (jQuery(this).val() === 'Uncheck All') {
        jQuery('#country input:checkbox').removeAttr('checked');
        jQuery(this).val('Check All');
    } else {
        jQuery('#country input:checkbox').attr('checked', 'checked');
        jQuery(this).val('Uncheck All');
    }
});

Upvotes: 3

Views: 222

Answers (1)

fiddle Demo

Change

jQuery('#country input:checkbox').attr('checked', 'checked');

to

jQuery('#country input:checkbox').prop('checked', true);


Use .prop()

Read .prop() vs .attr()

Upvotes: 6

Related Questions