0

I think I have a simple problem here but my jquery is somewhat limited.

I'm using this script to check all the checkboxes in my table rows which are handled by datatables (including the hidden ones from deferred rendering)

It's working for the checking portion, but the unchecking is not working when I want to deselect the boxes. How can I tweak what I have to to uncheck the boxes correctly?

Heres my code:

$('#selectall').on('click', function() {  //on click 
        if(this.checked) { // check select status
            var cells = dTable.cells( ).nodes();
            $( cells ).find(':checkbox').prop('checked', $(this).is(':checked')); 
        } else {
            var cells = dTable.cells( ).nodes();
            $( cells ).find(':checkbox').prop('checked', $(this).is(':not(:checked)')); 
        }
    });

Thanks in advance

2 Answers 2

1

I'm partial to this version myself:

$('#selectall').on('click', function() {  //on click 
    var cells = dTable.cells( ).nodes();
    $( cells ).find(':checkbox').prop('checked',this.checked);         
});
Sign up to request clarification or add additional context in comments.

4 Comments

For minimization sake I'd like to use this, but does it uncheck when the selectall is unchecked?
that's definitely better. I wanted to illustrate the problem with his code but this way is much preferred :-)
I went ahead and marked you as correct, thanks for the help @billynoah yours also worked and helped me understand what I was doing wrong. Thanks to both of you. Wish I could mark you both correct.
@Habitat - this second argument of prop() sets the value. If $('#selectall') is not checked then this.checked evaluates to false which would mean the property would be changed to not checked. so.. yes, it should work.
1

Looks to me like your uncheck code evaluates to true.. which means it would be checking them. Try this instead:

$('#selectall').on('click', function() {  //on click 
    if (this.checked) { // check select status
        var cells = dTable.cells( ).nodes();
        $( cells ).find(':checkbox').prop('checked',true); 
    } else {
        var cells = dTable.cells( ).nodes();
        $( cells ).find(':checkbox').prop('checked',false); 
    }
});

Comments

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.