»
EnglishFrenchVietnamese

Print - Simple JavaScript for Auto-Sum with Checkboxes - JavaScriptBank.com

Full version: jsB@nk » Form » Checkbox » Simple JavaScript for Auto-Sum with Checkboxes
URL: http://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html

Simple JavaScript for Auto-Sum with Checkboxes © JavaScriptBank.comThis JavaScript code example uses a for loop to calculate the sum of JavaScript checkbox values. This JavaScript will display a running total automatically when visitors click to select an item without reloading the current page. Very easy to edit and implement into your existing web forms.

Full version: jsB@nk » Form » Checkbox » Simple JavaScript for Auto-Sum with Checkboxes
URL: http://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html



JavaScript
<script type="text/javascript">// Created by: Jay Rumsey | http://www.nova.edu/~rumsey/// This script downloaded from JavaScriptBank.comfunction 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>


HTML
<input type="checkbox" id='game0' value="9.99"  onclick="UpdateCost()">Game 1 ( 9.99)<br><input type="checkbox" id='game1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br><input type="checkbox" id='game2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br><input type="checkbox" id='game3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br><input type="checkbox" id='game4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>