Skip to main content
Participant
November 7, 2022
Answered

Hide/show text field with checkbox

  • November 7, 2022
  • 2 replies
  • 1036 views

Hi everyone

I am a graphic designer, Java Script is not my best friend.

I am trying to create a form where the check boxes hide / show text fields.

I have very simple code (in a Java Script document, not under the checkbox) that doesn't work ... Why ?! Please help me. Thank you in advance!

 

check2 is the name of the check box

text2 is the name of the text field.

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

var aaa = this.getField("check2");

if (aaa.value == "Yes")
{
this.getField("text2").display = display.hidden;
}

if (aaa.value == "Off")
{
this.getField("text2").display = display.visible;

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

 

Best

Maciek

This topic has been closed for replies.
Correct answer try67

You can't put it only there. If you do, it needs to be in a function, and that function needs to be called from the field. I recommend using Thom's code from above.

2 replies

try67
Community Expert
Community Expert
November 7, 2022

You're missing a closing curly bracket at the end, but I assume that's a copy&paste error, or the code wouldn't be accepted at all.

Where did you place the code?

Did you check the JS Console after it executed, to see if there are any error messages?

The code itself seems fine, but if you didn't enter the field names or values correctly, for example, that would trigger an error.

Participant
November 8, 2022

Thank you very much for your reply. 

Yes... Copy/paste error. 

I placed the code in the main Document JavaScripts. Not in CheckBox's window with Run JavaScript Action

No errors under console. Just not working 🙂 

 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 8, 2022

You can't put it only there. If you do, it needs to be in a function, and that function needs to be called from the field. I recommend using Thom's code from above.

Thom Parker
Community Expert
Community Expert
November 7, 2022

Delete your current script and place this code into the MouseUp Action of the checkbox. 

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

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
November 7, 2022

Thank You very much! Yes. It works. But I am wondering if it is possible to do it not from CheckBox, but from main Document JavaScripts - from Prepare Form Tool.