I have to add values for all the checkbox that are selected. I would like to use a for loop to achieve this.
But the code is not working and I believe it's because of the way that I am doing the concatenation.
I have tried the following but neither works:
window.document.forms[2].i.checked == true
window.document.forms[2].${i}.checked == true
This is my code:
HTML:
<form class="jumbotron form3">
<h5>EXERCICE 7</h5>
<p>Veuillez cocher trois cases:</p>
<hr>
<input type="checkbox" name="1" value="-8"> Case 1
<br><input type="checkbox" name="2" value="5"> Case 2
<br><input type="checkbox" name="3" value="3"> Case 3
<br><input type="checkbox" name="4" value="10"> Case 4
<br><input type="checkbox" name="5" value="0"> Case 5
<br><input type="checkbox" name="6" value="-1"> Case 6
<br><input type="checkbox" name="7" value="-7"> Case 7
<br><input type="checkbox" name="8" value="15"> Case 8
<br><input type="checkbox" name="9" value="8"> Case 9
<br><input type="checkbox" name="10" value="3"> Case 10
<hr>
<input type="button" class="btn btn-warning" value="Calculer le score du jeu" onClick="calculerScore()">
<input type="button" class="btn btn-info" value="Reset">
<br><br>
<input type="text" name="score">
</form>
JS:
calculerScore = () => {
var resultat = 0;
for (let i = 1; i <= 10; i++) {
if (window.document.forms[2].i.checked == true) {
resultat += Number(window.document.forms[2].i.value);
}
}
window.document.forms[2].score.value = resultat;
}
.ishould be[i]