/    Sign up×
Community /Pin to ProfileBookmark

simple calculating table problem

Hi

I’ve got a table that needs to calculate the total from a combination of values generated from dropdowns and free text fields.

This is working okay but I’m getting the dreaded “NaN” in the total field until I enter a value in that text field and move off it.

Also I want to right align the values, display them all as currency to two decimal places.

Any help is appreciated

Cheers

Luigi

[code=php]
<html>
<head>
<title>computer</title>
<script type=”text/javascript”>
function calcTotal()
{
document.f.grandTotal.value =
parseInt(document.f.totComputer.value) +
parseInt(document.f.totDesk.value) +
parseInt(document.f.totChair.value) +
parseInt(document.f.other.value);
}
function setValue(name,src)
{
document.f[name].value = src.value;
calcTotal();
}
</script>
</head>
<body onload=”setValue(‘totComputer’,document.f.computer); setValue(‘totDesk’,document.f.Desk); setValue(‘totChair’,document.f.Chair); setValue(‘totChair’,document.f.other);”>
<form name=f method=”POST” action=”–WEBBOT-SELF–“>
<div align=”left”>
<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”307″>
<tr>
<td width=”71″>computer</td>
<td width=”161″><select size=”1″ name=”computer” onchange=”setValue(‘totComputer’,this);”>
<option value=1000>small</option>
<option value=2000>medium</option>
<option value=3000>large</option>
</select></td>
<td width=”196″><input type=”text” name=”totComputer” size=”20″></td>
</tr>
<tr>
<td width=”71″>desk</td>
<td width=”161″><select size=”1″ name=”Desk” onchange=”setValue(‘totDesk’,this);”>
<option value=100>wood</option>
<option value=200>glass top</option>
<option value=300>4 drawer</option>
</select></td>
<td width=”196″><input type=”text” name=”totDesk” size=”20″></td>
</tr>
<tr>
<td width=”71″>chair</td>
<td width=”161″><select size=”1″ name=”Chair” onchange=”setValue(‘totChair’,this);”>
<option value=80>highback</option>
<option value=40>lowrise</option>
</select></td>
<td width=”196″><input type=”text” name=”totChair” size=”20″></td>
</tr>
<tr>
<td width=”71″>other</td>
<td width=”161″>
&nbsp;</td>
<td width=”196″><input type=”text” name=”other” onBlur=”setValue(‘other’,this);” size=”20″></td>
</tr>
<tr>
<td width=”71″></td>
<td width=”161″>
<p align=”left”>total:</td>
<td width=”196″><input type=”text” name=”grandTotal” size=”20″></td>
</tr>
</table>
</div>
<p><input type=”submit” value=”Submit” name=”B1″><input type=”reset” value=”Reset” name=”B2″></p>
</form>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JuuitchanApr 15.2004 — FIrst, why are you using parseInt?Also, you appear to have an error here:

<body onload="setValue('totComputer',document.f.computer); setValue('totDesk',document.f.Desk); setValue('totChair',document.f.Chair); setValue('totChair',document.f.other);">

You repeated totChair. Is this a mistake?

What is your currency? Dollars?

In another thread, I posted a routine for converting numbers to strings showing the numbers as fixed-point with 2 decimal places. Here is that thread:

http://forums.webdeveloper.com/showthread.php?s=&threadid=32530
Copy linkTweet thisAlerts:
@LuigiXauthorApr 15.2004 — Hi Juuitchan

The repeated totChair was indeed a mistake.

Currency is dollars

I tried the code in the other thread without success.

If you could help a little more I'd really appreciate it. Actually on reflection its not necessary to go to 2 decimal places, but would like all numbers in dollars and for the grandtotal to work with the combination of dropdowns and free text fields.

Thanks for your assistance

Cheers

Luigi
×

Success!

Help @LuigiX 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.16,
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,
)...