make js loop unlimited
I got the following script
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i = 0; i < 5; i++) {
gn = 'game' + i;
elem = document.getElementById(gn);
if (elem.checked == true) {
sum += Number(elem.value);
}
}
document.getElementById('totalcost').value = sum.toFixed(2);
}
</script>
<input type="checkbox" id='game0' value="10" onclick="UpdateCost()">Game
1 ( 9.99)<br>
<input type="checkbox" id='game1' value="10" onclick="UpdateCost()">Game 2
(19.99)<br>
<input type="checkbox" id='game2' value="10" onclick="UpdateCost()">Game 3
(27.50)<br>
<input type="checkbox" id='game3' value="10" onclick="UpdateCost()">Game 4
(45.65)<br>
<input type="checkbox" id='game4' value="10" onclick="UpdateCost()">Game 5
(87.20)<br>
<input type="text" id="totalcost">
Now when i add checkboxes it is no longer working. the same happens when i
delete checkboxes.
This has something to do with(I think):
for (i=0; i<5; i++)
How can i change this so that i can have only 1 checkbox or 1000 so
basically any amount i want to create.
No comments:
Post a Comment