2

I have a function that is supposed to catch changes to all checkboxes. It works when the boxes are clicked manually, but does not work when the toggleAll() function triggers them.

Anyway to correct this?

$(function() {
    $(":checkbox").change(function(){
    if ( this.checked ) {
        alert(this.id+' is checked');
    } else {
        alert(this.id+' is not checked');
    }
    });
});

function toggleAll() {
    var selectAllCheckbox = $("#selectAllCheckbox");
    if ( selectAllCheckbox.prop('checked') ) {
        // uncheck them all
        $("[id^=friendRequestCheckbox]").attr('checked', false);
    } else {
        // check them all
        $("[id^=friendRequestCheckbox]").attr('checked', true);
    }
}

I am running an old version of jQuery and have not updated to the new version including prop, just in case that's relevant.

1 Answer 1

3

When changing elements programmatically, you also have to trigger the event programmatically. Try :

$("[id^=friendRequestCheckbox]").trigger('change')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, very simple and elegant solution. Will mark as answer as soon as it lets me. Thanks for the prompt answer.

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.