I’ve been working with an excellent script that executes a sum based upon a ID declaration in <td>
Unfortunately the script adds zeros into the total fields. Could any one here nudge me in the right direction as to how to tell the script to not print show zeros in rows without a calculation? It fine if they appear on lines that contains numbers for the sum.
Thanks for the help. Below shows the script in action.
[code=html]
<html>
<head>
<script type=”text/javascript”>
function totFields(){
puts=document.getElementById(“r1”).getElementsByTagName(“input”).length
rowCount=0
while(document.getElementById(“r”+rowCount)){
subTotal=0
currentRow=document.getElementById(“r”+rowCount)
rowInputs=currentRow.getElementsByTagName(“input”)
for(var i=0;i<rowInputs.length;i++){
if(rowInputs[i].type==”text”&&rowInputs[i].name.indexOf(“subtotal”)==-1){
subTotal+=rowInputs[i].value*1
}
}
document.getElementById(“subtotal”+rowCount).value=subTotal
rowCount++
}
/* Sutotal gets a zro OnChange here I belive this is where the problem resides. */
total=0
for(var j=0;j<rowCount;j++){
total+=document.getElementById(“subtotal”+j).value*1
}
document.getElementById(“gtotal”).value=total
}
</script>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ /><style type=”text/css”>
<!–
body {
margin-left: 50px;
margin-right: 50px;
margin-top: 50px;
margin-bottom: 50px;
}
–>
</style></head>
<body>
<form method=”POST” action=”” id=”totForm”>
<p>
</p>
<h3>OnChange form calculation. Total prints "0" Problem</h3>
<p></p>
<table width=”900″ colspan=”1″>
<tr bgcolor=”#F3F3F3″>
<td class=”center”>MON</td>
<td class=”center”>TUE</td>
<td class=”center”>WED</td>
<td class=”center”>THR</td>
<td class=”center”>FRI</td>
<td class=”center”>SAT</td>
<td class=”center”>SUN</td>
<td class=”center”>Total</td>
</tr>
<tr bgcolor=”#F3F3F3″ id=”r0″>
<td class=”center”><input type=”text” size=”3″ name=”MON1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”TUE1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”WED1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”THU1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”FRI1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SAT1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SUN1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”form[subtotal0]” id=”subtotal0″ /></td>
</tr>
<tr bgcolor=”#F3F3F3″ id=”r1″>
<td class=”center”><input type=”text” size=”3″ name=”MON2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”TUE2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”WED2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”THU2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”FRI2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SAT2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SUN2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”form[subtotal1]” id=”subtotal1″ /></td>
</tr>
<tr bgcolor=”#F3F3F3″ id=”r2″>
<td class=”center”><input type=”text” size=”3″ name=”MON3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”TUE3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”WED3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”THU3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”FRI3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SAT3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SUN3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”form[subtotal2]” id=”subtotal2″ /></td>
</tr>
<tr bgcolor=”#F3F3F3″>
<td colspan=”4″> </td>
<td class=”center”> </td>
<td class=”center”> </td>
<td class=”center”>Grand Total:</td>
<td class=”center”><input type=”text” size=”3″ name=”gtotal” id=”gtotal” /></td>
</tr>
</table>
</form>
<p>If you enter a number in the upper right field you will see zeros appear in all total fields. This is not wanted. How can I have them not appear on rows with no calculation?</p>
</body>
</html>