Selective HTML form text field adder
where 1 is a common number across all fields to be added, and where total1 is a field to save the total to. Work in progress to solve this common problem.
totalTheTotals(a,b,c) adds the value of a and b and puts it in c.
1 2 <script language="javascript" type="text/javascript"> 3 4 function selectiveTotal(form, which) 5 { 6 var total = 0; 7 for(var j = 0; j < form.elements.length; j++){ 8 var cid = form.elements[j].id.toString(); 9 if ((form.elements[j].type == "text") && 10 (form.elements[j].value.length > 0) && 11 (cid.charAt(cid.length-1) == which) && 12 (cid != "total" + which)) 13 { 14 total += parseInt(form.elements[j].value); 15 } 16 } 17 document.getElementById("total" + which).value = total; 18 totalTheTotals("1","2","3"); 19 } 20 21 function totalTheTotals(a, b, c) 22 { 23 var total = parseInt(document.getElementById("total" + a).value) + parseInt(document.getElementById("total" + b).value); 24 document.getElementById("total" + c).value = total; 25 } 26 27 </script> 28