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

Hide text if content in another text field is blank

Community Beginner ,
Oct 03, 2017 Oct 03, 2017

I'm very new to JavaScript and need help hiding text in the field below if another field does not have content. In other words, I don't want acct_api: to be visible if field acctno1 is blank.


event.value="acct_api: " + this.getField("acctno1").valueAsString;

TOPICS
PDF forms
9.4K
Translate
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
Community Expert ,
Oct 03, 2017 Oct 03, 2017

Use this:

var v = this.getField("acctno1").valueAsString;

if (v=="") event.value = "";

else event.value = "acct_api: " + v;

Edit: Forgot to add the fixed text in the code...

View solution in original post

Translate
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 ,
Oct 03, 2017 Oct 03, 2017

Use this:

var v = this.getField("acctno1").valueAsString;

if (v=="") event.value = "";

else event.value = "acct_api: " + v;

Edit: Forgot to add the fixed text in the code...

Translate
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 Beginner ,
Oct 03, 2017 Oct 03, 2017

It works! Thank you so much.

Translate
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 ,
Oct 03, 2017 Oct 03, 2017

Can you help me?  I've been going around with this forever. Every time you or someone posts an answer to this leave blank scenario I try changing mine and it still shows 0%.

The % calculation works but it won't stay blank if there is no value in the field being calculated.  Since we are using a form that we want to be filled out online but still have the option for manual it is important that the field be blank if someone is printing it out. Not all completed forms will include a change in $ and %.  The Amount of Change field I got to stay blank but not the percentage.  This is what I have in the custom calculation currently.  I appreciate any help you can offer.  Thanks.

var v1 = +this.getField("Proposed Pay Rate").value;

var v2 = +this.getField("Current Pay Rate").value;

if (v2=="") event.value="";

else event.value =

// compute difference between v1 and v2 as a percentage of v2;

event.value = (v1 - v2) / v2

Translate
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 ,
Oct 03, 2017 Oct 03, 2017

Try this code:

var v1 = +this.getField("Proposed Pay Rate").value;

var v2 = +this.getField("Current Pay Rate").value;

if (v2==0) event.value="";

else event.value = (v1 - v2) / v2;

Translate
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 ,
Oct 03, 2017 Oct 03, 2017

Thanks for your speedy reply.  Unfortunately, it still shows the 0.00% when the other fields are empty.

Translate
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 ,
Oct 03, 2017 Oct 03, 2017

A field with the Percent format can't be empty. You can set it to have no format and add the percentage symbol yourself, either in the calculation script, or (better yet) in a custom Format script.

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

Do you have the script you suggest to format the number to percent?  I can't find one that works.

Thanks

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

The Format script is pretty simple:

if (event.value!="") event.value += "%";

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

Thanks, but that is one of the ones not working. 

var v1 = +this.getField("Proposed Pay Rate").value; 

var v2 = +this.getField("Current Pay Rate").value; 

if (v2==0) event.value=""; 

else event.value = (v1 - v2) / v2;

if (event.value!="") event.value += "%";

...is the whole script I have. With that extra bit that is supposed to make the answer a percentage I only get the error message. Before adding that extra line at the bottom it gave me a .50 instead of 50%. 

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

Use this:

var v1 = +this.getField("Proposed Pay Rate").value;

var v2 = +this.getField("Current Pay Rate").value;

if (v2==0) event.value="";

else event.value = (((v1 - v2) / v2) * 100)  + "%";

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

That gives the same result. Same error message.

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

Change the field's Format to None.

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

Thank you. That was it. When I got rid of the percentage format I left it at number. None works now. Thanks so much.

Translate
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 ,
Jun 03, 2020 Jun 03, 2020
LATEST

To make your code easier to read and debug, I recommend doing the following:

  • replace all occurrences of "v1" with "proposedPayRate"
  • replace all occurrences of "v2" with "currentPayRate"

"proposedPayRate" and "currentPayRate" are more mnemonic.

 

Translate
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 Beginner ,
Oct 12, 2017 Oct 12, 2017

Please refer to my original request above.

So I've run into an issue with these fields because they are calculations. In my experience, calculations often require the user to tab through 1 or more text fields before the total/answer populates the form. In this case, my Adobe Acrobat PDF is being populated by content from a different software application. As such, these fields are not populating unless the user clicks ANY checkbox or tabs through ANY field. This works great for PDFs that require the user to fill in additional fields, however I'm concerned if the user doesn't need to fill anything out on the form because these fields are not populating content. Is there any thing I can do to make this work?

Thanks for your help!

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

If the user needs to tab or edit another field before your calculations execute correctly it means there's something wrong with them. Most likely, the field calculation order is incorrect.

But the situation you describe where the values are populated from an external source is actually a valid one. The solution for it is to embed a doc-level script that executes the following command:

this.calculateNow();

That will force all the calculations to happen when the file is first opened.

Translate
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 Beginner ,
Oct 12, 2017 Oct 12, 2017

Will this be entered in the Validate/Run custom validation script?

Translate
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 ,
Oct 12, 2017 Oct 12, 2017

No. It's a doc-level script, not a field-level script. It should be entered under Tools - JavaScript - Document JavaScripts.

Translate
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 Beginner ,
Oct 12, 2017 Oct 12, 2017

Amazing! It works great. Thank you!!

Translate
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