These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Code for simple sum calculation

isaanderson
Registered: Aug 11 2008
Posts: 72
Answered

I am trying to do a form in acrobat that contains a simple sum calculation.

How can I calculate the sum of 16 different fields and display the total inside a different text field?

These are the names of the fields I want to sum:

total1, total2, total3, total4, total5, total6, total7, total8, total9, total10, total11, total12, total13, total14, total15, total16

And this is the name of the field that I want to display the total of the sum of the 16 fields above:

TotalWeighted

I know I can do this by using the sum feature in acrobat but I do not want to do that because it has caused me trouble in the past. I want to do it with JavaScript.

Programmers please help!

Thanks in advance.

My Product Information:
Acrobat Pro 8.1.3, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You can use the following code as the custom calculation script for the TotalWeighted field:

(function () { // Initialize variable to hold the sum of the fieldsvar sum = 0; // Loop through the "total" fields...for (var i = 1; i < 17; i += 1) { // Add current field value (as a number) to the sumsum += +getField("total" + i).value;} // Set this field's value to the sumevent.value = sum; })();

George
JohnnyRiv
Registered: Dec 2 2008
Posts: 9
Hi isaanderson,

I guess you could do the following in the Custom Calculation Script:

var t1 = this.getField("total1").value;
var t2 = this.getField("total2").value;
var t3 = this.getField("total3").value;
event.value = t1+t2+t3

Just repeat the var statements for each of your Total fields, then add the variables to your event.value line.

Enjoy,
Johnny

Acrobat 5, 6 Pro, 7 Pro, 8 Pro, 9 Pro Extended, LC 7, 8 & LC ES 8.2

isaanderson
Registered: Aug 11 2008
Posts: 72
George, thanks for responding so fast. Your code works perfect. Now, I have one more favor to ask. I have one more problem with the form. Every time you make a change inside one of the fields, like you change one of the numbers for a new one, Acrobat requires the user to click outside of the field to recalculate the total, which will be a pain for my client, just because they are not used to do forms and tend to forget to click outside of the field and end up with the wrong total.

Now the favor I need from you in order to somehow make them click somewhere else every time they make a change. I want to add a button that when clicked will display the total inside the field named "TotalWeighted", in another words: I don't want the total field to display the sum amount until the button is released.

How can I add a "on release" function to the code you gave me?

The button name is "totalbtn"
and the field I want to display the total sum when the button is release is called "TotalWeighted"

Thanks again. I appreciate your help.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
If you want to place the code in a button's Mouse Up event, you should first get rid of the existing calculation script and use the following code for the button:

(function () { // Initialize variable to hold the sum of the fieldsvar sum = 0; // Loop through the "total" fields...for (var i = 1; i < 17; i += 1) { // Add current field value (as a number) to the sumsum += +getField("total" + i).value;} // Set the TotalWeighted field's value to the sumgetField("TotalWeighted").value = sum; })();

though I think that clicking outside of a field, tabbing out, or hitting Enter is simpler. If they never click the button, your script won't execute and an incorrect total will be displayed.

You could instead set up a "Click Here" button that does nothing other than give them something to click.

George
isaanderson
Registered: Aug 11 2008
Posts: 72
George_Johnson,
With all the Holiday parties I had no chance to thank you for providing the code for the button's Mouse Up event. It worked perfect and my client was very happy with the final form.

Thank you very much!