Reputation: 6519
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");
});
})
});
Now is adding that class to all tr parents even if I have one checbox selected ...
Upvotes: 0
Views: 1334
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
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
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");
});
});
});
Upvotes: 1