views:

465

answers:

4

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.

A: 

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

Doug D
like this ? if (!$("#S"+num).filter(':disabled'))this doesn't work, the active buttons don't get checked.
FALCONSEYE
A: 

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'));
}
});
FALCONSEYE
A: 

It might be better UI to use a toggle button to "check all/uncheck all" instead of a checkbox for "ALL". So 1 button + 4 checkboxes instead of 5 checkboxes. Then you won't have to mess with disabling.

Ray
The page is being created from a db output. Each record has different available options, so i had to disable the checkboxes to indicate that option is not available for that particular record. Not sure, if toggle option would work in my situation.
FALCONSEYE
A: 

umm i would probably do this:


    $('input[name=all]').click(function(){
        var classn = $(this).attr('class');
        $('.'+classn+':not(:disabled)').attr('checked', $(this).is(':checked'));
    });

just assign a uniform class for every row say row_one, row_two and so on.

uji