0

below is my code:

Cell.setAttribute('type', 'checkbox');
Cell.setAttribute('name', "Size");

based on condition I will set some values to 'checked' and 'disabled'.

Cell.setAttribute('checked', 'true');
Cell.setAttribute('disabled', 'true');   

Here 'Size' contains some values which are disabled, both unchecked and checked.

So whenever I am trying to retrieve the checked values, the default disabled checked boxes are also being returned.

I just want the checked values; not the disabled checked values in SelectedDetails method:

var testCaseArray =[]; 
var checkbox = document.getElementsByName('Size');

for(var i=0; i<checkbox.length; i++){
    if(checkbox[i].checked){ 
        testCaseArray.push(checkbox[i].value);
    }
}

return testCaseArray;
ness-EE
  • 1,334
  • 13
  • 19

3 Answers3

1

Retrieve all checked checkbox elements in a series, except the disabled one(s), into an Array:

var chkd = [].slice.call(document.querySelectorAll('input:checked'))
            .filter(function(e) {
               return null == e.getAttribute('disabled');
             });

You can also use this selector to retrieve a Array-like list with the same selection:

document.querySelectorAll('input:checked:not(disabled)')

jsFiddle

KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

You need to add in the check for disabled, as well as the check for it being checked

var testCaseArray =[]; 
var checkbox = document.getElementsByName('Size');

for(var i=0; i<checkbox.length; i++){
    if(checkbox[i].disabled){
        continue;
    }
    if(checkbox[i].checked){ 
        testCaseArray.push(checkbox[i].value);
    }
}

return testCaseArray;
ness-EE
  • 1,334
  • 13
  • 19
0

Since JavaScript has short-circuit evaluation, just add the condition to your if statement:

var testCaseArray = [],
    checkbox = document.getElementsByName('Size');

for (var i = 0; i < checkbox.length; ++i) {
    if (!checkbox[i].disabled && checkbox[i].checked) {
        testCaseArray.push(checkbox[i].value);
    }
}

return testCaseArray;

The JavaScript engine will automatically stop evaluating if !checkbox[i].disabled returns false.

Community
  • 1
  • 1
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156