Hope I am posting to the right place.

I am creating a form in Acrobat that needs a little Javascript, but I don't know anything about JavaScript so any help will be appreciated.

In the form there are for separate price fields and a field that automatically fills in an average price for all of them. The problem is not all 4 price fields are going to be filled out in every instance which ruins the average. I need a script the will average the Price fields that have a value.

I was thinking I could create an invisible field that simply state that

If (Price) > 0 then set value to 1 (what would this look like in JavaScript?)

I could do that for each of the 4 price fields then in another invisible field count them and use that sum to divide the Total cost to get the average.

Thanks in advance for any help!

8 Years
Discussion Span
Last Post by OmarKhatib

Honestly, I don't know what works in Acrobat and I'm about to go to bed so don't have time to play with the software to find out ...

First you need to get a hook on the form fields .. in html you might use an element id for this.
Then the simple test if a value is in the field ... but I don't know what Acrobat returns if the field is empty (null, false, '', ...) to perform this test.
Also, I don't know if Acrobat returns only string-types or also number types ... and the following relies on DOM support ????

In html it might look like this -- maybe someone else has direct experience in this environment.

<script type="text/javascript">
  var ttl = 0;
  var field1 = document.getElementById('field1_id');
  var field2 = document.getElementById('field2_id');
  if ( field1 ) { // is not null ...
    ttl += parseInt( field );
  if ( field2 ) {
    ttl += parseInt( field );
  alert( ttl ); // display the results in an alert box

We could also parse through all fields in a loop, but I really don't know how Acrobat handles this sort of thing?

Sorry, maybe this will work but I wouldn't count on it. ;-)

How about looking in the Acrobat documentation or on the Adobe website?


Thanks for the help. The script that worked is:

// Initialize variables 
var cur_val; // variable to hold current field value 
var sum = 0; // Sum of field values 
var num = 0; // Number of fields with values 

// Loop through Price fields 
for (var i = 0; i < 4; i++) { 

    // Get field value 
    curr_val = getField("Price" + (i + 1)).valueAsString; 

    // If field is not empty, add it to the sum and increment counter 
    if (curr_val != "") { 
        sum += +curr_val; // Add current value to sum 
        num++; // Increment counter 

// If there is at least one field with a value 
// Set this field value equal to the average 
if (num) event.value = sum/num; 

// Otherwise, blank this field 
else event.value = "";

I got the help from the Adobe forums. The Acrobat documentation assumes you know JavaScript.

Thanks again for the help!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.