Hi all,
I've created a table and it works just fine when you insert values in the text field. When you insert "0" as a value it still works, problem is if you skip a text field and don't insert anything it returns "NaN" in the total column. I can't figure out what to change. Any help would be much apprecitated.
Also, and this would be a bonus, how do you insert a button to clear the entire field to start over if you wanted to without having to delete every entry.
Thanks,
HTML Code:
Javascript code:
I've created a table and it works just fine when you insert values in the text field. When you insert "0" as a value it still works, problem is if you skip a text field and don't insert anything it returns "NaN" in the total column. I can't figure out what to change. Any help would be much apprecitated.
Also, and this would be a bonus, how do you insert a button to clear the entire field to start over if you wanted to without having to delete every entry.
Thanks,
HTML Code:
Code:
<table width="500" border="1" align="center">
<tr bgcolor="#CCCCCC">
<td width="340" style="font-weight: bold">Current Housing Expenses</td>
<td width="144"> </td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
</tr>
<tr>
<td>Utilities (if paid seperate)</td>
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
</tr>
<tr bgcolor="#CCCCCC">
<td style="font-weight: bold">Current Non-Housing Expenses</td>
<td> </td>
</tr>
<tr>
<td>Food</td>
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
</tr>
<tr>
<td>Clothing</td>
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
</tr>
<tr>
<td>Day Care / Tuition</td>
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
</tr>
<tr>
<td style="font-weight: bold">Savings</td>
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
</tr>
<tr bgcolor="#CCCCCC">
<td style="font-weight: bold">Total Monthly Expenses and Savings</td>
<td><input type="text" id="tot" /></td>
</tr>
</table>
Code:
<script type="text/javascript">
function calcTotal(txtBox, totBox)
{
var totVal;
try
{
totVal = document.getElementById(totBox).value;
if(totVal!= null && totVal!='')
{
document.getElementById(totBox).value= eval(parseInt(document.getElementById(totBox).value) + parseInt(txtBox.value));
}
else
{
document.getElementById(totBox).value= txtBox.value;
}
}
catch(e)
{}
}
</script>
Comment