Skip to main content
Participant
May 4, 2020 11:57 PM UTC
Answered

Fill in another text field based on previously entered text

Hey there,

I am struggling a bit with my code, as I do not get it to work with my form. The current code I have is as follows:

var field1 = this.getField("Brand").valueAsString;

if (field1=="Samsung") event.value = "SM-G960F";

else if (field1=="Apple") event.value = "A2111";

else event.value = "";


The steps I followed were:

1) I created a text field named "Model" and made it "Read Only"

2) Under "Javascript", I created a new document JavaScript called "Model"

3) I filled in the code above, where the goal is as follows:

     - The code would start by retrieving the text field "Brand" to see what value has been filled in (i.e.   "Samsung"

    - If "Samsung" has been filled in, the "Model" field should display the string "SM-G960F"
    - If "Apple" was entered in the "Brand" field, the "Model" field should display the string "A2111"

    - If nothing has been filled in, then the field should stay empty.

The current error is as follows (I've cycled through quite a bit now):

 

TypeError: this.getField(...) is null
1:Document-Level:Model

 

But this seems strange to me, as the getField value is supposed to be null until it is filled in right? Or am I completely off with my thoughts?


I would be very grateful if one of you guys could help me out! Here are some screenshots as well:

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
May 5, 2020 5:44 AM UTC

Use the script as calculation of field "Model".

3 replies

Participant
Jun 1, 2020 3:34 PM UTC

Thanks both! Because of these answers, I realised my form wasn't in the correct mode (if anyone else is struggling: go to prepare form -> More -> Convert to form), which allowed me to se the calculation field. Thanks again!

Translate
Thom Parker
Community Expert
Community Expert
May 5, 2020 3:49 PM UTC

What it's telling you is that the field name is incorrect. It is the "getField" function that is returning null, not the value. 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
May 5, 2020 5:44 AM UTC

Use the script as calculation of field "Model".

Translate