8 Replies Latest reply on Apr 25, 2014 7:44 AM by kyleneedhelp

# Help Write Javascript Calculation

Ok here is the trouble I am having. I want to write a javascript calculation to sum the total of text fields when a certain choice is selected in a dropdown field.

The text fields I want to sum are called : Amount 1, Amount 2...all the way to Amount 14

The Dropdown is called: Payment 1, Payment 2... all the way to Payment 14

So, I want to write the calculation tha sums the amount fields that correspond with the same number dropdown when the drop down choice selected is "Reimbursement"

So if amount 1 is \$50, and payment 1 is reimbursement it adds to the total, but if something else is selected instead it just adds \$0.

• ###### 1. Re: Help Write Javascript Calculation

You can use this code as the custom calculation script of your total field:

var total = 0;

for (var i=1; i<=14; i++) {

if (this.getField("Payment "+i).value=="Reimbursement") {

total += Number(this.getField("Amount "+i).value);

}

}

event.value = total;

• ###### 2. Re: Help Write Javascript Calculation

Awesome thanks!

I have run into another dilemma though. I also have the function where I can add a page with more lines. I need the box to also calculate if a page is added. The fields are then Amount 15-39 and Payment 15-39. The template is uses is called "add expenses". Is this possible?

• ###### 3. Re: Help Write Javascript Calculation

Are you spawning the template, or just showing it, though?

• ###### 4. Re: Help Write Javascript Calculation

Spawning

• ###### 5. Re: Help Write Javascript Calculation

That's much more complicated, because the names of the fields in the spawned pages are not just "Amount 15", but a combination of the page number, template name and original field name, so a much more complex script is required to handle those as well.

• ###### 6. Re: Help Write Javascript Calculation

Yes. Once the page is spawned the name of the fields are "P1.Add Expenses.Amount 15", "P1.Add Expenses.Amount 16... to 39" and "P1.Add Expenses.Payment 15"...39.  Is this possible?

• ###### 7. Re: Help Write Javascript Calculation

I guess I could reset it to show page 2 instead. that probably makes it easier huh?

• ###### 8. Re: Help Write Javascript Calculation

Although, I don't know how to do that really either. I really have written this whole document with the help of internet resources/these forums.