Skip to main content
Participant
Nov 7, 2022 11:38 PM UTC
Answered

Hide/show text field with checkbox

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Nov 8, 2022 12:08 AM UTC

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.

6 replies

try67
Community Expert
Community Expert
Nov 7, 2022 11:50 PM UTC

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.

Translate
Participant
Nov 8, 2022 12:02 AM UTC

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 🙂 

 

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Nov 8, 2022 12:08 AM UTC

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.

Translate
Thom Parker
Community Expert
Community Expert
Nov 7, 2022 11:49 PM UTC

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;

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
Nov 7, 2022 11:57 PM UTC

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. 

Translate