I have content being iterated, and then outputted to HTML, from javascript obj data. I am trying to incorporate a check box, that when clicked; will 'select all' or 'check all' of my other checkboxes (these other checkboxes are coming from my JavaScript code, from iterated for loop).
attempt:
function checkAll(checkId){
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox" && inputs[i].id == checkId) {
if(inputs[i].checked == true) {
inputs[i].checked = false ;
} else if (inputs[i].checked == false ) {
inputs[i].checked = true ;
}
}
}
}
JS obj data to the HTML content:
var json =[{
"Name": "zips",
"Type": "Directory",
"DateModified": "6/14/2018 17:22:50",
"Size": "5 KB",
}, {
"Name": "presets",
"Type": "Directory",
"DateModified": "5/11/2018 7:32:10",
"Size": "2 KB",
}, {
"Name": "workflow",
"Type": "Directory",
"DateModified": "6/26/2018 10:29:59",
"Size": "6 KB",
},{
"Name": "software",
"Type": "Directory",
"DateModified": "3/16/2018 10:29:59",
"Size": "16 KB",
},{
"Name": "mmm_data",
"Type": "Directory",
"DateModified": "6/27/2018 1:19:29",
"Size": "3 KB",
},{
"Name": "jobs",
"Type": "Directory",
"DateModified": "4/27/2018 11:59:59",
"Size": "3 KB",
},
];
var string ="";
for (i in json) {
string +='<div class="row"><div class="col-md-15 col-sm-1"><input type="checkbox" name="chk[]"></div><div class="col-md-15 col-sm-4"><span class="folders">'+json[i].Name+'</span></div><div class="col-md-15 col-sm-3"><span class="directory">'+json[i].Type+'</span></div><div class="col-md-15 col-sm-3"><span class="date-stamp">'+json[i].DateModified+'</span></div><div class="col-md-15 col-sm-1"><span class="date-size">'+json[i].Size+'</span></div></div>';
};
document.getElementsByClassName('update-data')[0].innerHTML =string
static HTML:
<div class="data-columns">
<div class="row" id="hd">
<div class="col-md-15 col-sm-1">
<input type="checkbox" onclick="checkAll('chk');" >
</div>
<div class="col-md-15 col-sm-4">
<div id="named" class="sr">Name</div>
</div>
<div class="col-md-15 col-sm-3">
<div id="type" class="sr">Type</div>
</div>
<div class="col-md-15 col-sm-3">
<span id="dated" class="sr">Date Modified</span>
</div>
<div class="col-md-15 col-sm-1">
<span id="size" class="sr">Size</span>
</div>
</div>
<div class="update-data">
</div>
</div>

inputs[i].id == checkIdwhat is with that check in the if?