I need to fill in the second column with value 50 when the CheckBox is checked and with value 0 when the CheckBox is unchecked.
I seems that document.getElementsByName("val" + i).value
doesn't work properly or I missed something.
EDIT I replaced document.getElementsByName("val" + i)[0].value
with document.getElementsByName("val" + i).value
and I introduce if
condition inside the loop
function myFunction() {
var checkBox = document.getElementById("myCheck");
var table = document.getElementById("myTable");
var i;
for (i = 0; i < table.rows.length; i++) {
if (checkBox.checked == true) {
document.getElementsByName("val" + i)[0].value = "50";
} else {
document.getElementsByName("val" + i)[0].value = "0";
}
}
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
Checkbox: <input type="checkbox" id="myCheck" onclick="myFunction()">
<table id="myTable">
<tr>
<th>Interval</th>
<th>Valori</th>
</tr>
<tr>
<td>Int 1</td>
<td><input name="val1" value size="5"></td>
</tr>
<tr>
<td>Int 2</td>
<td><input name="val2" value size="5"></td>
</tr>
<tr>
<td>Int 3</td>
<td><input name="val3" value size="5"></td>
</tr>
<tr>
<td>Int 4</td>
<td><input name="val4" value size="5"></td>
</tr>
<tr>
<td>Int 5</td>
<td><input name="val5" value size="5"></td>
</tr>
</table>