Skip to main content
gamzes93533812
Known Participant
February 21, 2018
Answered

button on click show/hide other fields

  • February 21, 2018
  • 1 reply
  • 9141 views

Hi,

I have a button created on my Acrobat Form, and I would like to use this button to show/hide some other fields.

So basically, if user click once, it will display some other fields, and if clicks for the second time it will again hide fields.

If it was a checkbox, I could simply assign a mouse up event as follows to solve the issue:

this.getField("myfieldtoshowhide").display = (event.target.value=="Off") ? display.hidden : display.visible;

But which kind of script and event is required for button? I simply could not solve it out.

This topic has been closed for replies.
Correct answer Thom Parker

You just need to put your existing script in to the MouseUp for the button and modify it. And of course you also need to add a hidden checkbox to the form.

var oChkFld = this.getField("HiddenCheck");

oChkFld.checkThisBox(0,!oChkFld.isBoxChecked(0));

this.getField("myfieldtoshowhide").display = (oChkFld.value=="Off") ? display.hidden : display.visible;

1 reply

Thom Parker
Community Expert
Community Expert
February 21, 2018

To do this you need to save a "state" value.  A checkbox works because it maintains the "state" as the checkbox value. With a button there is no "state", as you've already found out. An easy solution is to  use a document level variable, but if the form is saved and then reopened the "state" is lost because the document variable value is lost. So if it is important for this to work after a document is closed, then you need a way to save the "state" with the document. The best solution is to use a hidden check box.  Each time the button is clicked it toggle the value of the hidden check and sets the visibility accordingly, just like you're script above

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
gamzes93533812
Known Participant
February 22, 2018

Thank you for making the reasons very clear. Yes, it's important for this to work after the doc is closed!

I wanted to apply the "hidden check box" idea. Forgive my ignorance but how to assign the button on click function that will check/uncheck dependent checkbox?

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 22, 2018

You just need to put your existing script in to the MouseUp for the button and modify it. And of course you also need to add a hidden checkbox to the form.

var oChkFld = this.getField("HiddenCheck");

oChkFld.checkThisBox(0,!oChkFld.isBoxChecked(0));

this.getField("myfieldtoshowhide").display = (oChkFld.value=="Off") ? display.hidden : display.visible;

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