Skip to main content
Known Participant
June 5, 2025
Answered

Showing text box based on whether text is typed in another text box

  • June 5, 2025
  • 1 reply
  • 686 views

I have three text boxes named Text1, Text2 and Text3.  I have another three text boxes named TextA, TextB and TextC.  If text is typed into TextA, I want Text1 to be visible and Text2 and Text3 to be hidden.  If text is typed into TextB, I want Text2 to be visible and Text1 and Text3 to be hidden.  If text is typed into TextC, I want Text3 to be visible and Text1 and Text2 to be hidden.

I've tried using the following script, which works for four text boxes (Text1, Text2, Text A, TextB), but I can't modify to work when I add Text3 and TextC:

 

if (event.target.value == "") {
this.getField("Text2").display = display.hidden;
}
else {
this.getField("Text2").display = display.noPrint;
}


if (event.target.value == "") {
this.getField("Text1").display = display.noPrint;
}
else {
this.getField("Text1").display = display.hidden;
}

Correct answer PDF Automation Station

Remove both format scripts and enter the following custom calculation script into another text field (you can make it hidden):

var a=this.getField("TextA").value;
var b=this.getField("TextB").value;
var t1=this.getField("Text1");
var t2=this.getField("Text2");
var t3=this.getField("Text3");
if(!a && !b)
{
t1.display=display.noPrint;
t2.display=display.hidden;
t3.display=display.hidden;
} else
if(a && !b)
{
t1.display=display.hidden;
t2.display=display.noPrint;
t3.display=display.hidden;
}
else
{
t1.display=display.hidden;
t2.display=display.hidden;
t3.display=display.noPrint;
}

I would suggest another approach however:

Make only one print button and change the label (button.setCaption field method) and the print range, depending on the values of TextA and TextB.

 

Your form attempts to set visiblity to "noPrint" but your request says "hidden".  If you are clearer you will get better help.

1 reply

Thom Parker
Community Expert
Community Expert
June 5, 2025

Where is this script placed? It makes all the difference in the world. 

 

In general "event.target.value" is not used with value based scripts, i.e., testing the value of a field. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
June 5, 2025

I need to revise my original post.  I have Text1, Text2 and Text3, but only TextA and TextB (not TextC).  If text is entered in TextA, I want Text2 to be visible and Text1 and Text3 to be hidden.  If text is typed into TextB, I want Text3 to be visible and Text1 and Text2 to be hidden.  If both TextA and TextB are blank, I want Text1 to be visible and Text2 and Text3 to be hidden.

The script in my original post is placed in Custom Format Script of TextB.  It works as expected if I just have Text1, Text2 and TextA.  I can't figure out how to expand to add Text3 and TextB.

There would never be a time when text is entered into TextB but not TextA as TextB is a continuation page if TextA is full.

I have attached a copy of the pdf file.

PDF Automation Station
Community Expert
Community Expert
June 5, 2025

Format scripts should not be used in this way.  A format script shows a display value which is not necessarily the value property of the field.  You should use either use a calculation script in the field that is changing, or a validation script in the field that is changing it.  For your reference:

https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts

https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts-eb5

https://pdfautomationstation.substack.com/p/another-method-for-calculation-vs

https://pdfautomationstation.substack.com/p/custom-format-for-pdf-fields