Forum Moderators: open
The called function is someting like:
function amounttotal() {
var myTotal;
myTotal=parseInt(myTotal,10);
myTotal='';
if (document.getElementById('first_amount')) {
myTotal = parseInt(myTotal) + parseInt(document.getElementById('first_amount').value);
}
if (document.getElementById('second_amount')) {
myTotal = parseInt(myTotal) + parseInt(document.getElementById('second_amount').value);
}
if (document.getElementById('third_amount')) {
myTotal = parseInt(myTotal) + parseInt(document.getElementById('third_amount').value);
}
document.getElementById('general_total').value = parseInt(myTotal);
}
As long as not ALL the detail fields are filled, I get a NaN in the general total.
I would get rid of that, especially since a user must NOT enter an amount in all the detail fields to get his total displayed.
I want the general total to be calculated and displayed even if only a part of the detail amount fields have a value filled in.
OK, so I searched the Internet, and found that I could test each detail value with something like:
if (isNaN(document.getElementById('first_amount'))==false) {
myTotal = parseInt(myTotal) + parseInt(document.getElementById('first_amount').value);
}
By the way, I had to add the parseInt thing because otherwise I only had a concatenation, even if only numbers were entered.
Any idea what I missed in this code to make it work, even if only 1 of the detail fields receives a value?
[color=brown]// myTotal declared (not set)[/color]
var myTotal;
[color=brown]// myTotal = parseIn(*undefined*,10) // --> NaN[/color]
myTotal=parseInt(myTotal,10);
[color=brown]// line above was pointless, because now..[/color]
myTotal=''; and since
parseInt('',10) [color=brown]--> Nan[/color] ..it's all over from then on!
*! Replace ¦¦ in code with unbroken pipes (or it'll go wrong)
function amounttotal()
{
var total = 0, D = document;
var fieldIDs = ['first_amount','second_amount','third_amount'];for(var k=0;k<fieldIDs.length;k++)
{
[color=brown]// if p(value) is 0 or Nan, then 0 is added[/color]
total += parseInt( D.getElementById(fieldIDs[k]).value,10) [red]¦¦[/red] 0;
}D.getElementById('general_total').value = total; [color=brown]// implicit: number -> string[/color]
}
[edited by: Bernard_Marx at 2:10 pm (utc) on Feb. 14, 2005]
Your reply is not very clear to me. When I enter amounts in ALL the detail fields, the total field displays the correct sum.
I understand that I'm creating an *undefined* variable at the top of the script, but when I delete the line:
myTotal=parseInt(myTotal,10);
And as I said, I want the total to be calculated even if only part of the details are entered.
In conclusion, the *undefined* starting value declaration is not affecting the script.
Any other ideas?
The reason why I have the "if variable exist" statements all over the script, is because this form is generated on the fly depending on selections on earlier pages.
Thus I don't know in advance which amount detail fields I'll have (I just named them "first", "second" for my post here, but they have other names AND ID's in reality), and there could be about 12 of them.
Do you know what the names might be?
or do you need to iterate through all the form's text fields in a more generic fashion.
PS see sticky
------
This should do it, if you know the full set of field ids that can appear:
function amounttotal()
{
var total = 0, D = document;
var fieldIDs = ['first_amount','second_amount','third_amount'];
var [color=brown]field[/color];for(var k=0;k<fieldIDs.length;k++)
{
field = D.getElementById(fieldIDs[k]);
if(field) total += parseInt( field.value ,10 ) ¦¦ 0;
}D.getElementById('general_total').value = total;
}
This function totals all the amounts from any field with the className, 'amount';
The function requires that you pass a reference to the form itself.
eg: onsubmit = "ammounttotal(this)"
function amounttotal(form)
{
var fieldClass = "amount";
var fields = form.elements, field, k=0;
var total = 0;while(field=fields[k++])
{
if(field.className==fieldClass)
total += parseInt( field.value ,10 ) ¦¦ 0;
}D.getElementById('general_total').value = total;
}