Simple totalling of a column with onChange()

C

charliefortune

Every time a field in an accounts form changes, an onChange =
"updateAmount()" executes this function -

function updateAmount(x){
var field = 'amount' + x;
var val = parseFloat(document.forms[0].field.value);
document.forms[0].totAmount.value = val;
}

the fields are called 'amount1', 'amount2', 'amount3' etc. and the
onChange passes that integer. I want to calculate a new total every
time a new amount is entered.

1. Is there a way to avoid having to pass the 'x' index. Surely the DOM
knows which field is provoking the function call.

2. The parseFloat doesn't seem to work, is my syntax correct ?

Thank you
 
R

Randy Webb

charliefortune said the following on 4/29/2006 2:20 PM:
Every time a field in an accounts form changes, an onChange =
"updateAmount()" executes this function -

function updateAmount(x){
var field = 'amount' + x;
var val = parseFloat(document.forms[0].field.value);

var val = +document.forms[0].elements['amount'+x].value;
document.forms[0].totAmount.value = val;
}

the fields are called 'amount1', 'amount2', 'amount3' etc. and the
onChange passes that integer. I want to calculate a new total every
time a new amount is entered.

Don't pass the integer, pass a reference to the field itself using the
'this' operator. But, for what you are doing, you don't need a reference
to the element at all.

Do a test:

Fill out the form, go back and change the first input to a new number,
your results will be wrong.
1. Is there a way to avoid having to pass the 'x' index. Surely the DOM
knows which field is provoking the function call.

It doesn't matter in this case, you need to re-total all the fields
every time one changes.
2. The parseFloat doesn't seem to work, is my syntax correct ?

No.

Try this:

function getTotal(){
formRef = document.forms[0]
var totalFields = formRef.elements.length
var totalCounter = 0
for (var i=0;i<totalFields;i++)
{
if (formRef.elements.name.indexOf('amount') == 0)
{
totalCounter += +formRef.elements.value
}
}
formRef.elements['totAmount'].value = totalCounter
}

It depends on any field being calculated starting with the string "amount".
 
C

charliefortune

thank you, got it going now. I am multiplying the values by one to
convert to a number, are there any problems in doing this ? It seems
easier than parseFloat()

totalCounter += (formRef.elements.value)*1;
 
R

Randy Webb

charliefortune said the following on 5/1/2006 7:52 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

thank you, got it going now. I am multiplying the values by one to
convert to a number, are there any problems in doing this ? It seems
easier than parseFloat()

It's not a matter of "easier" but rather of efficiency.
totalCounter += (formRef.elements.value)*1;


totalCounter += +formRef.elements.value;

Use the unary + operator as it seems to be the most efficient way found
yet to convert a string to a Number.
 
C

charliefortune

ah, I took that out because i thought it was a typo. Thanks for all
your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top