Skip to main content
Known Participant
November 23, 2016
Answered

How to sum only visible fields?

  • November 23, 2016
  • 5 replies
  • 1299 views

I'm using Acrobat Pro DC. My form has a column of 12 randomly named fields (product names) with dollar values. Each field is made visible or hidden with it's own checkbox action. I want to sum the column, but only the fields that are visible that have the boxes checked. I need a script that will determine which fields are visible, then sum them in a separate "Total" field. I don't know Javascript, but can do basic script editing make them work for me, like adding field names.

I am guessing this is pretty basic, but thanks for your any help you can provide!

This topic has been closed for replies.
Correct answer George_Johnson

It would be easiest if you use a custom calculation script for the Total field. The following is incomplete, but should get you started:

// Custom calculation script for Total field

// Initialize variable

var sum = 0;

// Add first field value if its corresponding check box is selected

if (getField("item1_checkbox").value !== "Off") {

    sum += +getField("product1").value;

}

// Add second field value if its corresponding check box is selected

if (getField("item2_checkbox").value !== "Off") {

    sum += +getField("product2").value;

}

// Repeat for other 10 values

// Set this field value to the sum, or set to blank if zero

if (sum > 0) {

    event.value = sum;

} else {

    event.value = "";

}

You will have to replace the field names used with the getField calls with the actual field names you're using. Be sure to use the exact field names (case matters) and check for errors by displaying the JavaScript console (Ctrl + J). This script could be made more compact and efficient, but it should get you started.

5 replies

Inspiring
November 23, 2016

If you set the value to sum for the item to 0, zero, for the unselected state and the value when selected you can sum the values.

Known Participant
November 23, 2016

Sorry, but I have no idea what you just said . I don't know anything about scripting. I'm going to need a script i can copy & paste into the javascript field in Acrobat

George_JohnsonCorrect answer
Inspiring
November 23, 2016

It would be easiest if you use a custom calculation script for the Total field. The following is incomplete, but should get you started:

// Custom calculation script for Total field

// Initialize variable

var sum = 0;

// Add first field value if its corresponding check box is selected

if (getField("item1_checkbox").value !== "Off") {

    sum += +getField("product1").value;

}

// Add second field value if its corresponding check box is selected

if (getField("item2_checkbox").value !== "Off") {

    sum += +getField("product2").value;

}

// Repeat for other 10 values

// Set this field value to the sum, or set to blank if zero

if (sum > 0) {

    event.value = sum;

} else {

    event.value = "";

}

You will have to replace the field names used with the getField calls with the actual field names you're using. Be sure to use the exact field names (case matters) and check for errors by displaying the JavaScript console (Ctrl + J). This script could be made more compact and efficient, but it should get you started.