TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Check if group of checkboxes are checked but not disabled

This code:

if ($('[id*="cat' + cat_id + '_sk"]').is(':checked') && $('[id*="cat' + cat_id + '_sk"]').not(':disabled')) {
        $('[id*="cat' + cat_id + '_sk"]').prop('checked', false);
} else if ($('[id*="cat' + cat_id + '_sk"]').not(':checked') && $('[id*="cat' + cat_id + '_sk"]').not(':disabled')) {
        $('[id*="cat' + cat_id + '_sk"]').prop('checked', 'checked');
}   

Seems to ignore the second part of the if clause; If a checkbox is checked I want it to be ignored.

I have tried it also doing is(':disabled) with a preceding !, also tried for a boolean !(element).prop('disabled'); -- all to the same effect.

The code above is definitely called because ALL checkboxes will toggle checked state -- the question revolves around the inability of ignoring the disabled checkboxes.

Is my logic wrong here?

Upvotes: 0

Views: 228

Answers (4)

Ilia Ross
Ilia Ross

Reputation: 13412

You should use !obj.is(':checked')), example:

if ($('[id*="cat' + cat_id + '_sk"]').is(':checked') && $('[id*="cat' + cat_id + '_sk"]').not(':disabled')) {
     $('[id*="cat' + cat_id + '_sk"]').prop('checked', false);
} else if (!$('[id*="cat' + cat_id + '_sk"]').is(':checked') && $('[id*="cat' + cat_id + '_sk"]').not(':disabled')) {
     $('[id*="cat' + cat_id + '_sk"]').prop('checked', 'checked');
}   

Another example, if you want to reach unchecked checkboxes after you check checked, just separate you logic, example:

if ($('[id*="cat' + cat_id + '_sk"]').is(':checked')) {
    if ($('[id*="cat' + cat_id + '_sk"]').not(':disabled')) {
         $('[id*="cat' + cat_id + '_sk"]').prop('checked', false);
    } 
} else {
    if ($('[id*="cat' + cat_id + '_sk"]').not(':disabled')) {
         $('[id*="cat' + cat_id + '_sk"]').prop('checked', 'checked');
    }   
}

Upvotes: 0

indubitablee
indubitablee

Reputation: 8206

something like this? http://jsfiddle.net/swm53ran/133/

if($(this).is(':checked') && $(this).attr('disabled') != 'disabled')

the above line of code is the logic that says if this checkbox is checked and not disabled

$(document).ready(function() {
    $('.check').each(function() {
        console.log($(this).attr('disabled'));
        if($(this).is(':checked') && $(this).attr('disabled') != 'disabled') {
            $(this).after('Checked and not disabled');
        }
    });
});

<input type="checkbox" class="check" checked disabled/><br/>
<input type="checkbox" class="check" /><br/>
<input type="checkbox" class="check" checked disabled/><br/>
<input type="checkbox" class="check" checked /><br/>
<input type="checkbox" class="check" /><br/>
<input type="checkbox" class="check" disabled /><br/>
<input type="checkbox" class="check" /><br/>

hope this helps

Upvotes: 0

rwacarter
rwacarter

Reputation: 2004

This should work a little better:

$('[id*="cat' + cat_id + '_sk"]').each(function(elm) {

    var $elm = $(elm);

    if($elm.is(':checked') && !$elm.is(':disabled')) {
         $elm.removeProp('checked');
    }
    else if(!$elm.is(':checked') && !$elm.is(':disabled')) {
         $elm.prop('checked', 'checked');
    }

});

As A. Wolff says above, .is(':checked') will return true even only one checkbox in matched set is checked. You need to loop through the elements using each.

Upvotes: 2

tabz100
tabz100

Reputation: 1473

The .not() in jQuery is only a filter that reduces the current set of elements. It does not return a boolean if it is disabled.

Upvotes: 1

Related Questions