Closed Thread Icon

Topic awaiting preservation: having trouble adding a bunch of values! Pages that link to <a href="https://ozoneasylum.com/backlink?for=8317" title="Pages that link to Topic awaiting preservation: having trouble adding a bunch of values!" rel="nofollow" >Topic awaiting preservation: having trouble adding a bunch of values!\

 
Author Thread
hecster2k
Nervous Wreck (II) Inmate

From: sj, ca, usa
Insane since: Feb 2002

posted posted 09-18-2002 23:15

ok, i have this form that has 10 fields in it. to make it simple, i named these fields T1, T2, .. , T10. also added a code block as so, to do a grand total calculation:

code:
function getInvoiceTotal()
{
total = 0;
for(i=1;i<35;i++)
{
total += eval("document.forms[0].T" + i + ".value");
}
document.forms[0].grandTotal.value = total;
}



for each of these Tx values, i have another function that calculates these by doing a (qty * individualCost). you might say that it's similar to an invoice sheet. that does individual cost, quantity, subtotal, and grand total. but every time i add in a new value, it just appends it to the end of the grandTotal, instead of actually adding it to a total. If i do a parseFloat, i get NaN as the result.

what am i doing wrong? thanks!

"there has to be a solution."

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-18-2002 23:56

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-19-2002 10:50
quote:
eval("document.forms[0].T" + i + ".value");



Hec, that is not such a good method to access these form variables!

try this:
total += parseFloat(document.forms[0]['T'+i].value);

beside that you said you have 10 fields, and your code reads:

quote:
for(i=1;i<35;i++)



35 ?!


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-19-2002 17:16

Lallous: your code should also work, but sometimes using eval() on a form value is desired, since it allows the user to enter things like "3+5" and get 8. Of course, it also opens the script up to errors, but sometimes that's not a concern.

« BackwardsOnwards »

Show Forum Drop Down Menu