jeudi 28 mai 2015

jQuery: Check / uncheck all siblings with certain class

I am new to jQuery and hope someone here can help me with this question.

I am trying to set up an "All" checkbox to check / uncheck all its sibling checkboxes (i.e. those under the same div / parent) - but only if they DO NOT have a certain class ("other").

My approach was to cause the least run time and to write this in a way that it can also be applied to similar structures in other divs.

The code I have works without the :not() selector but I can't get it to work when I include this. Also, I am not sure if my approach is the best / fastest way here.

My jQuery:

$('.checkAll').click(function(){
    if($(this).is(':checked')){
        $(this).siblings(":not($('.other'))").prop('checked', true);
    }else{
        $(this).siblings(":not($('.other'))").prop('checked', false);
    }
});

Many thanks in advance for any help, Mike




Aucun commentaire:

Enregistrer un commentaire