Skip to main content
Participating Frequently
July 7, 2021
Answered

Populate form field name from text box

  • July 7, 2021
  • 3 replies
  • 971 views

Hi, 

 

I've looked and haven't found the easiest path to making my text field auto populate initials for a checklist.  I have about 185 fields over 3 columns and know there must be an easier way than adding this script to each form field individually.

// check form field, if blank add initials, if initials make N/A, if N/A make blank

var initialsField= this.getField("RIC2").valueAsString;
if(initialsField==""){
this.getField("RIC2").value=this.getField("RICInitials").value;
}
else if (initialsField==this.getField("RICInitials").value){
		this.getField("RIC2").value= "N/A";
}
else if (initialsField=="N/A"){
		this.getField("RIC2").value= "";
}

Initially, I was going to paste that in the action tab with the on focus trigger and change the form field each time but I thought programmers are way smarter than that but I can't figure out what to do next.

 

I thought I could make a function in the main javascript part, I think that is called document level.  Then I saw something about event.target.value but can't figure out how to make either work.

 

If you don't mind, all I want to change above is instead of putting the field name, some snippet of code that says "for your field name" or "take action in your field" so I dont have to change it to say "field1", "field2", "field3", etc.  With that I can select all of the fields applicable and add all of the same action on focus run a javascripts at the same time.

 

Thank you for any guidance.

This topic has been closed for replies.
Correct answer Nesa Nurani

If you use it  as "On focus" you can use this code and it will work in all fields:

var Initials = this.getField("RICInitials").valueAsString;
if(event.value == ""){
event.target.value = Initials;}
else if(event.value == Initials){
event.target.value= "N/A";}
else if(event.value == "N/A"){
event.target.value= "";}

3 replies

Thom Parker
Community Expert
Community Expert
July 7, 2021

The code you've shown is circular. Each time it is run it will change the field to the next value, going around in a circle.

Your explanation is a bit muddled, but it doesn't seem that this is what you are after.

 

The best thing for you to do is explain, as simply as possible, in clear, explicit, and complete language, exactly how you want this to work.  If you have multiple rows of fields that require the same behavior, then also explain how the fields are named.  This type of arrangment is best handled if the fields have a consistent naming scheme. 

Then we can suggest a solution.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
joeyp2021Author
Participating Frequently
July 7, 2021

Thank you sir, I wanted it to be circular.  

 

It's a checklist that some of my team has to use and one of my flight chiefs stated that manually typing in his initials in each block was tedious so this was an idea to click on a box and it autofills your initials.  If that was a mistake, click on it again for it to go back to N/A or click again for it to become blank again.

 

I thought to use "on focus" so it works when tabbing through but I would like it to work with clicking multiple times in the text box without focusing elsewhere and coming back.  What would you suggest for that?  Thank you for the insight.

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
July 7, 2021

If you use it  as "On focus" you can use this code and it will work in all fields:

var Initials = this.getField("RICInitials").valueAsString;
if(event.value == ""){
event.target.value = Initials;}
else if(event.value == Initials){
event.target.value= "N/A";}
else if(event.value == "N/A"){
event.target.value= "";}

joeyp2021Author
Participating Frequently
July 7, 2021

Thank you ma'am, this is perfect.  I'm new but really enjoying learning the abilities of PDFs and learning coding.  Thank you!

Bernd Alheit
Community Expert
Community Expert
July 7, 2021

Why does you use the on focus trigger?

On which field does you use it?