• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
4

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

Community Beginner ,
Aug 30, 2023 Aug 30, 2023

Copy link to clipboard

Copied

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!

 

TOPICS
JavaScript , PDF forms

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 30, 2023 Aug 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.....];
}

 

 

Votes

Translate

Translate
Community Expert ,
Aug 30, 2023 Aug 30, 2023

Copy link to clipboard

Copied

To do a comparison use "==", not "=" 

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 30, 2023 Aug 30, 2023

Copy link to clipboard

Copied

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.   

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 30, 2023 Aug 30, 2023

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 31, 2023 Aug 31, 2023

Copy link to clipboard

Copied

Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 01, 2024 Oct 01, 2024

Copy link to clipboard

Copied

Thom,

 

I know this is an older thread, and I'm sorry for reopening it.  However, could you explain to me how to use this validation so that if any data is filled in a field, another field becomes visible?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2024 Oct 01, 2024

Copy link to clipboard

Copied

Yes, it's all in this article:

https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 01, 2024 Oct 01, 2024

Copy link to clipboard

Copied

LATEST

Thank You!  Bookmarked for current and future reference!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines