Skip to main content
Known Participant
May 4, 2018
Question

Show/Hide One Field Based on Multiple Fields

  • May 4, 2018
  • 2 replies
  • 1982 views

Hey everyone,

I have a form with a text field "Total". I'd like "Total" to be revealed if any one of ten other text fields are filled in. So if all ten fields are blank, "Total" is hidden. If any one of "Price_PP_1", "Price_PP_2", "Price_PP_3", "Price_PP_4", "Price_PP_5", "Price_PP_6", "Price_PP_7", "Price_PP_8", "Price_PP_9", or "Price_PP_10" are filled in with any info at all, "Total" shows itself.

It seems like it should be simple. I just don't know how to write the custom calculation script that basically tells it, "If this or this or this or this or this happens, show it. If none of it happens, don't show it."

Can anyone help?

This topic has been closed for replies.

2 replies

BarlaeDC
Community Expert
Community Expert
May 4, 2018

Hi,

The script only needs to be added to the calculate of the Total field, as all calculate scripts are called when a field value is changed. The total field also needs to be called "Total" or you need to change the following line.

var myTotal = curDoc.getField("Total"); // replace Total with the name of your field.

Regards

Malcolm

Known Participant
May 4, 2018

Ahhhh, that may be a problem. I already have a calculation in the Total field. It is the basic sum of a few other fields on the form.

BarlaeDC
Community Expert
Community Expert
May 7, 2018

Hi,

Then the best plan would be to port the calculation in the total field to be included in the script above and placed on the Total field. If you post the calculation then we can probably help with this.

In general you want to keep the number of calculation scripts any file has small as they are called every time any field changes and this means that the more scripts you have the longer it takes, and the more possibility that you are calculating a given value more than once.

if you have 2 fields, field1 and field2, and a total field, if you put the calculation on both of the fields and not the total, then every time a value changes you will calculate the total field twice. The more fields the more times you run the exact same calculation.

Regards

Malcolm

BarlaeDC
Community Expert
Community Expert
May 4, 2018

Hi,

You should be able to use something like the following to be able to show hide the field.

var totalValue = null

var curDoc = app.doc;

// do this for each field ( could be in a loop if you want)

var myField = curDoc.getField ("Price_PP_1").value;

if ( myField !== "")

{

     // have added parseInt here just in case you don't have any formatting on the field to stop non numbers being used

     totalValue += parseInt ( myField, 10);

}

var myTotal = curDoc.getField ("Total");

// if totalValue has changed from the above code, then we can show the field

if ( totalValue !== null)

{

     myTotal.hidden = false;

}

else

{

     myTotal.hidden = true;

}

myTotal.value = totalValue;

Hope this helps

Malcolm

Known Participant
May 4, 2018

Thank you for your help, but it didn't work. Or at least I didn't work it correctly. I planned on doing this to each Price_PP_ field. I go to custom calculation JavaScript Editor and enter...

--------------------------------------------------------------------------------

var totalValue = null

var curDoc = app.doc;

var myField = curDoc.getField ("Price_PP_1").value;

if ( myField !== "")

{

     totalValue += parseInt ( myField, 10);

}

var myTotal = curDoc.getField ("Total");

if ( totalValue !== null)

{

     myTotal.hidden = false;

}

else

{

     myTotal.hidden = true;

}

myTotal.value = totalValue;

--------------------------------------------------------------------------------

... I don't get any error in the JavaScript Editor, but I do get an error message in Acrobat. As soon as I go back to my form Acrobat pops up with, "The value entered does not match the format of the field [ Total ]". As I click around on the form in any field at all I get this error message.

What step did I miss along the way here?