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

Show/Hide one field, why doesn't my Javascript work?

New Here ,
Oct 21, 2017 Oct 21, 2017

I'm using Acrobat DC Pro.

I am trying to show or hide a field based on the calculated total that appears in a field named NVCA Raw Score. The calculated field has possible numbers of 0 - 7. If the calculated field displays number 0 - 4, I want to hide the FLAGGED field. If the calculated fields displays numbers 5, 6, or 7, I want the hidden field to display. I've tried my best to cobble some JavaScript together from other scripts using < >, but the hidden field never displays. I've learned a lot from this group, but still need help every once in a while. Any help will be very appreciated.

var nResult = this.getField("NVCA Raw Score").value;

if ( nResult >= 4 ) {

this.getField("FLAGGED").display = display.visible;

}

else if ( nResult <= 4 ) {

this.getField("FLAGGED").display = display.hidden;

}

Many thanks,

Charlie

charlie6067

TOPICS
Acrobat SDK and JavaScript , Windows
1.9K
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

correct answers 1 Correct answer

Community Expert , Oct 30, 2017 Oct 30, 2017

A validation script doesn't make sense in this context since it only gets triggered when the value of that field is changed (by the user!), and this is a read-only field, so it never happens.

Move the code to be a custom calculation script and change it to:

var nFlag = Number(this.getField("NVCA Raw Score").value);

if ( nFlag >= 5 ) {

    event.target.display = display.visible;

} else {

    event.target.display = display.hidden;

}

Translate
LEGEND ,
Oct 21, 2017 Oct 21, 2017

Where do you put the 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 21, 2017 Oct 21, 2017

There is nothing visibly wrong with your code. To figure out what's going wrong I'd start off by running it from the console window. I almost always develop code in the console window first, to make sure it does what I think it does. If you can change the switching value and then run the code and see the field hide/show, then you know the code is correct, but isn't getting run at the right time.

So yes, script location is very important.

The second thing I'd do is place a "console.println()" statement in the code so you can watch the Console window know exactly when the code is (or isn't) executed. And tell what value (nResult) is being used.

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

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 21, 2017 Oct 21, 2017

There is one problem with your code. Both conditions cover the scenario where the value of the field is exactly 4. That is not a desired situation. You need to decide what should happen in that case and remove the "=" part from the other condition.

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 30, 2017 Oct 30, 2017

Thom and others - Thank you for your replies. I've still not been able to figure out why the JavaScript won't show or hide the Flagged field based on the score in another field. I've attached the part of the form I'm having problems with. If you would please take a minute to look at the validate code, you may quickly spot what I've failed to notice. I've tried the same code in a JavaScript module but still can't get it to show/hide the field.

Thanks,

Charlie 

Public link https://www.dropbox.com/s/47cjaoyxbiefgms/Example.pdf?dl=0

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 30, 2017 Oct 30, 2017

A validation script doesn't make sense in this context since it only gets triggered when the value of that field is changed (by the user!), and this is a read-only field, so it never happens.

Move the code to be a custom calculation script and change it to:

var nFlag = Number(this.getField("NVCA Raw Score").value);

if ( nFlag >= 5 ) {

    event.target.display = display.visible;

} else {

    event.target.display = display.hidden;

}

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 30, 2017 Oct 30, 2017
LATEST

It works. Thank you very much for your time and explanation.

Charlie

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