/    Sign up×
Community /Pin to ProfileBookmark

Total sum of 2 columns

I have 2 columns that auto sum to a subtotal. From there I need to add both subtotals together to get a grand total. I can’t figure out how to get the subtotals from the seperate functions to add together to get a grand total. Can someone please help?

JS Code:
<script type=”text/javascript”>
function calcTotal(txtBox, totBox) {
totBox = document.getElementById(totBox);

var totVal = Number(totBox.value);
var txtVal = Number(txtBox.value);

if ( isNaN(txtVal) ) {
alert(“Please only enter numbers.”);
txtBox.select();
txtBox.focus();
} else {
if ( isNaN(totVal) ) {
totVal = 0;
}
totBox.value = totVal + txtVal;

}

txtBox = null;
totBox = null;

}

function calcTotal(txt1Box, tot1Box) {
tot1Box = document.getElementById(tot1Box);

var tot1Val = Number(tot1Box.value);
var txt1Val = Number(txt1Box.value);

if ( isNaN(txt1Val) ) {
alert(“Please only enter numbers.”);
txt1Box.select();
txt1Box.focus();
} else {
if ( isNaN(tot1Val) ) {
tot1Val = 0;
}
tot1Box.value = tot1Val + txt1Val;

}

txt1Box = null;
tot1Box = null;

}
</script>

HTML Code:
<table width=”600″ border=”0″ align=”center” cellpadding=”5″>
<caption>
Borrower&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Co-Borrower
</caption>

<tr>
<td width=”321″>Credit Cards <span style=”font-size: small”>(Minimum Monthly Payments)</span></td>
<td width=”161″>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td width=”161″>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Car Loans <span style=”font-size: small”>(Minimum Monthly Payments)</span></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Student Loans</td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Installment Loans</td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Home Equity Loans</td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Alimony/Child Support</td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Other Monthly Debt</td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot’)” size=”20″ /></td>
<td>$<input type=”text” onblur=”calcTotal(this, ‘tot1’)” size=”20″ /></td>
</tr>
<tr>
<td>Monthly Subtotal</td>
<td>$<input type=”text” id=”tot” size=”20″ /></td>
<td>$<input type=”text” id=”tot1″ size=”20″ /></td>
</tr>
<tr>
<td style=”font-weight: bold”>Monthly Total</td>
<td colspan=”2″ style=”text-align: center”>$<input type=”text” id=”tot2″ size=”20″ /></td>
</tr>
</table>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 01.2009 — add the following code at the end of function [b]calcTotal[/b]
<i>
</i>document.getElementById('tot2').value = Number(document.getElementById('tot').value) + Number(document.getElementById('tot1').value)

it will take both subtotal and add it to Monthly Total. comments of your code: can't see the reason why you have two function named [b]calcTotal[/b], so the second function statement replace the first. also, the logic of your code is wrong. consider for example, type on every field [b]1[/b] and then keep pressing [b]Tab key[/b] on your keyboard. you will notice that the subtotal keep calculating addition even if you haven't changed the typed value on each fields.
Copy linkTweet thisAlerts:
@mrkhauthorApr 01.2009 — thank you very much! that was it
×

Success!

Help @mrkh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...