BurebistaRuler
BurebistaRuler

Reputation: 6519

Table tr td with checkboxes, apply parent tr class if no checkbox checked

I'm trying to add a small script to my table on this conditions: I have a table with a checkbox on each tr td and I want to apply my jQuery script only if selector find a tr where all td checkboxes are not checked but if only one checkbox is checked on tr row then do not add class to parent tr.

Basically I need to add a class to parent tr when on entire tr row none of td checkboxes is selected.

Here is my script:

$(function () {
    $('#callScr').on('click', function (e) {
        $(".table tr input:not(:checked)").each(function (i, obj) {
            $(this).parent().parent().addClass("yourClass");
        });
    })
});

Here is my fiddle:

Now is adding that class to all tr parents even if I have one checbox selected ...

Upvotes: 0

Views: 1334

Answers (3)

Gene R
Gene R

Reputation: 3744

i guess you want also remove class if checkbox will get checked:

$(function(){
    $('#callScr').on('click', function(e){
        $('.table tbody tr').each(function(){
            $(this).toggleClass('yourClass',$(this).find(':checkbox:checked').length == 0);
        });
    });
});

https://jsfiddle.net/DTcHh/16995/

Upvotes: 1

Joy Biswas
Joy Biswas

Reputation: 6527

You can iterate the tr's and do that instead and check the inputs inside

$(function(){
    $('#callScr').on('click', function(e){
       $( ".table tr:gt(1)" ).each(function(i, obj) {
          if($(this).find('input:checked').length===0)
             $(this).addClass( "yourClass" );
       });
    });
});

JS Fiddle for reference

Upvotes: 1

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

Loop through each tr in tbody and the length of checked checkbox in it. If length is 0 the add class.

$(function () {
    $('#callScr').on('click', function(e) {
        $(".table tbody tr").each(function(i, obj) {
            if ($(this).find(":checkbox:checked").length == 0)
                $(this).addClass("yourClass");
        });
    });
});

UPDATED FIDDLE

Upvotes: 1

Related Questions