Skip to main content
Participant
August 30, 2023
Answered

Need to show/hide specific fields depending on the value entered in a different field

  • August 30, 2023
  • 1 reply
  • 2962 views

I am creating a form in Adobe Acrobat Pro where you enter the number of agents, up to nine (and yes, I have the field format set to a number with zero decimals, and validate the value is in range 1-9), in the field "NumberOfAgents".  This will then make visible the lines for agents A - I (depending on number entered).  Each agent letter has the fields (that correlate to their letter) "A", "Agent_A_Name", "Agent_A_Title", "Agent_A_Signature", which need to switch from display.hidden to display.visible.  The fields "Agent_A_Name", and "Agent_A_Title" also need to go from required = false to required = true.  I added an action using the trigger Mouse Exit to run a JavaScript with the below.  However for some reason whenever I try to enter a value in "NumberOfAgents", it doesn't matter what I put in, the field changes the value to "1".   I'd also tried this as an On Blur trigger and had the same results.  

 

if (this.getField("NumberOfAgents").value = "1") {
     this.getField("A").display = display.visible;
     this.getField("Agent_A_Name").display = display.visible;
     this.getField("Agent_A_Title").display = display.visible;
     this.getField("Agent_A_Signature").display = display.visible;

     this.getField("Agent_A_Name").required = true;
     this.getField("Agent_A_Title").required = true;
     this.getField("B").display = display.hidden;
     this.getField("Agent_B_Name").display = display.hidden;
     this.getField("Agent_B_Title").display = display.hidden;
     this.getField("Agent_B_Signature").display = display.hidden;

     this.getField("Agent_B_Name").required = true;
     this.getField("Agent_B_Title").required = true;

     [all the way through Agent I];

} else if (this.getField("NumberOfAgents").value = "2") {
     this.getField("A").display = display.visible;
     this.getField("Agent_A_Name").display = display.visible;
     this.getField("Agent_A_Title").display = display.visible;

     [etc., etc., etc.....];

}

 

this continues all the way through to .value = "9", making another letter visible/required all the way through. 

 

I'm sure I'm doing something wrong here, but I can't quite figure out what.   

 

The good news is that with the "1" in the "NumberOfAgents" field, I am getting the desired results for the Agent A fields.   

 

Also, I'm guessing there's something better than the 496 lines I'm using to write all this, but my ability with JavaScript is very basic and I haven't been able to find a way to make it shorter through the days I've spent combing this board for clues/help/inspiration.  If anyone has any suggestions there, I'm open to them too.

 

Thanks for your help!

 

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

Actually, this scripts  should be a Custom Validation script on the "NumberOfAgents" field, because the validation event is called when the data in the field changes. 

Now the "if" code needs to be modified to use "event.value"

Like this:

if (event.value = "1") {
     this.getField("A").display = display.visible;

     [etc., etc., etc.....];
} else if (event.value = "2") {
     this.getField("A").display = display.visible;

     [etc., etc., etc.....];
}

 

 

1 reply

Thom Parker
Community Expert
Community Expert
August 30, 2023
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
KNic9903Author
Participant
August 30, 2023

Thank you so much!  I knew it was something simple!  I've read about == so many times, and one of these days that information will stick. 

 

That did the trick, and then once it was working I realized I needed it to be an On Blur trigger as well.   

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
August 30, 2023

Actually, this scripts  should be a Custom Validation script on the "NumberOfAgents" field, because the validation event is called when the data in the field changes. 

Now the "if" code needs to be modified to use "event.value"

Like this:

if (event.value = "1") {
     this.getField("A").display = display.visible;

     [etc., etc., etc.....];
} else if (event.value = "2") {
     this.getField("A").display = display.visible;

     [etc., etc., etc.....];
}

 

 

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