Check / uncheck all checkedboxes in a table

1

I have seen a similar post where you have a code that selects all the checkboxes in a table, but in my table if I have a checkbox with the attribute disabled this equals me.

function toggle(source) {
  checkboxes = document.getElementsByName('foo');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
      
    <input type="checkbox" name="foo" value="bar1" disabled> Bar 1<br/>
    <input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
    <input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
    <input type="checkbox" name="foo" value="bar4"> Bar 4<br/>
    </body>
    </html>

What condition can I add?

    
asked by MoteCL 04.01.2019 в 16:01
source

1 answer

2

Simply saying not to mark those that are disabled.

function toggle(source) {
  checkboxes = document.getElementsByName('foo');
  for(var i=0, n=checkboxes.length;i<n;i++) {
     if(!checkboxes[i].disabled){
        checkboxes[i].checked = source.checked;
       }
  }
}
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
      
    <input type="checkbox" name="foo" value="bar1" disabled> Bar 1<br/>
    <input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
    <input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
    <input type="checkbox" name="foo" value="bar4"> Bar 4<br/>
    </body>
    </html>
    
answered by 04.01.2019 / 16:27
source