Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Use formatting display to hide number form fields with value of zero

Explorer ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

I know I've seen this somewhere else in this forum, but I can't find it.

 

I have a number field that is calculated based on the value of two other fields. However, when the other fields are empty or the calculation otherwise equals zero, I want to hide the form field so that the zero is not visible. I think I've got the code right, but it doesn't seem to work. This is the code I'm using:

 

var field1 = this.getField("Field 1").value;
var field2 = this.getField("Field 2").value;
var total = field1 * field2;

if ( total!=0 )
{
  event.target.value = total;
  event.target.display = display.visible;
}
else
{
  event.target.display = display.hidden;
}

 

This code is used on the calculations for the field I want to hide. All three fields are specified as number fields. What am I missing? 

 

Edited to add: The field I want to hide is used in other fields and calculations, so I want to keep the zero value, but I want the field to appear blank if the value is zero. This is so the form can be printed and used manually.

 

Thanks in advance for any suggestions!

TOPICS
PDF forms

Views

8.0K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Explorer ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

It appears I have answered my own question. I've been struggling with this for hours, and after posting it here, the solution became clear. I simply switched the if/else statements. Instead of testing for when the field does not equal zero, I tested for when it does equal zero, and it hid everything correctly. Here's my updated code, in case it helps someone else.

var field1 = this.getField("Field 1").value;
var field2 = this.getField("Field 2").value;
var total = field1 * field2;

if ( total==0 )
{
  event.target.display = display.hidden;
}
else
{
  event.target.display = display.visible;
  event.target.value = total;
}

 

View solution in original post

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

The value needs to be set with event.value. 

 

event.value = total;

 

Otherwise your script looks correct. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

It seems to work with event.target.value, but I will change to event.value if that is more correct. Thanks for your help!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

It appears I have answered my own question. I've been struggling with this for hours, and after posting it here, the solution became clear. I simply switched the if/else statements. Instead of testing for when the field does not equal zero, I tested for when it does equal zero, and it hid everything correctly. Here's my updated code, in case it helps someone else.

var field1 = this.getField("Field 1").value;
var field2 = this.getField("Field 2").value;
var total = field1 * field2;

if ( total==0 )
{
  event.target.display = display.hidden;
}
else
{
  event.target.display = display.visible;
  event.target.value = total;
}

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

That's rediculous. It literally makes no difference. 

 

And for event.value, it's more important than you think that the values are set correctly. There is a specific order to events on the fields. The event.value property sets the value from the inside of the calcuation event. The event.target.value property references the field from the outside, it throws the event engine off, and triggers more events. This can cause problems with field formatting, other scripts, and slow everything down. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2020 Jan 10, 2020

Copy link to clipboard

Copied

I want to hide the form field so that the zero is not visible

 

You don't need to hide the field, just hide the value, without removing it so further calculations can occurs.

Place this as a Custom Format script in the "total" field:

if (event.value == 0) {event.value = ""};


Acrobate du PDF, InDesigner et Photoshopographe

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 09, 2020 Apr 09, 2020

Copy link to clipboard

Copied

Can you (or anyone else that could) help me to amend the above (or below code) to include negative numbers as well? I wish to hide "0" and any negative numbers.

 

if (event.value == 0) {event.value = ""};

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 09, 2020 Apr 09, 2020

Copy link to clipboard

Copied

if (event.value <= 0) {event.value = ""};

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 10, 2020 Apr 10, 2020

Copy link to clipboard

Copied

Thank you!!! Be Well!!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

Can I add code to the above script to show two decimals when the field is not blank?     Such as 19.50 instead of 19 for the result.

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

 

if (event.value <= 0) event.value = "";
else event.value = util.printf("%0.2f",event.value);

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2024 Feb 12, 2024

Copy link to clipboard

Copied

Hi! Is it possible to create a script that will hide a field value depending on the scenario?

 

For example, I'm creating a table that calculates average hours worked. In some scenarios, hours will only be entered in the 'Week 1 Hours' row. However, in other scenarios, hours will be entered in both 'Week 1 Hours' and 'Week 2 Hours' rows.

 

I'm wondering if it's possible to hide the value of the "Avg Total Hours" field IF no hours are entered in the 'Week 2 Hours' row? Essentially, I only want the average value to be visible if there are values in both the 'Total1' and 'Total2' fields.

 

Screenshot 2024-02-12 114928.pngexpand image

 

 

 

 

 

 

 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2024 Feb 13, 2024

Copy link to clipboard

Copied

Why Yes, it is possible with a script. Best to use the calculation script in either Total2 (sinces it's row is the trigger) or in the Avg Total Hours field. 

Read this article:

https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2024 Feb 13, 2024

Copy link to clipboard

Copied

LATEST

It might be simpler to just show a blank value when the output is zero. This can be done with a custom Format script. Format scripts do not chang ethe value of a field, they just change the display of a value. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 16, 2023 Nov 16, 2023

Copy link to clipboard

Copied

Thank you so much for sharing!  I am completely ignorant when it comes to code and your simple line worked for what I need!  

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

This works great, and so simple. thank you!

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines