Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery checkbox issue - do not check if it's disabled

I have 5 checkboxes in each row. The first one is 'ALL'. I am trying to see if any of the others are disabled. So, if somebody clicks on 'ALL' checkbox, I need to make sure the disabled ones are not checked. This is what I have:

("input[name^=all_]").each(function() {
var input = $(this);   
var name = input.attr('name');    
var num = /\d+$/.exec(name)[0]; 
$(this).click(function() {

if ($('"#G"+num').attr('disabled',false)) {            
$("#G"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ($('"#E"+num').attr('disabled',false)) {         
$("#E"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ($('"#W"+num').attr('disabled',false)) {
$("#W"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ($('"#S"+num').attr('disabled',false)) {
$("#S"+num).attr('checked', $("#all_"+num).is(':checked'));
}
});

});

The thing is, the disabled ones still gets checked once I click on 'ALL'. what am i doing wrong? thanks in advance.

like image 258
CFNinja Avatar asked Nov 12 '09 16:11

CFNinja


3 Answers

this works:

 if ( !$("#G"+num).is(':disabled') ) {             
$("#G"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ( !$("#E"+num).is(':disabled')) {            
$("#E"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ( !$("#W"+num).is(':disabled') ) {
$("#W"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if (!$("#S"+num).is(':disabled')) {
$("#S"+num).attr('checked', $("#all_"+num).is(':checked'));
}
});
like image 142
CFNinja Avatar answered Oct 31 '22 02:10

CFNinja


To check all except disabled

$('input:checkbox:not(:disabled)').attr('checked','checked');

To uncheck all except disabled

$('input:checkbox:not(:disabled)').removeAttr('checked');
like image 10
Serjas Avatar answered Oct 31 '22 03:10

Serjas


Use jQuery's ":disabled" filter instead of accessing the 'disabled' attribute.

like image 5
Doug Domeny Avatar answered Oct 31 '22 02:10

Doug Domeny