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

Javascript to autofill text fields

New Here ,
Jan 24, 2019 Jan 24, 2019

I want to be able to type a word in one text field and have several others auto populate with the letters of that word.  For example, I want to type the word "christmas" in the text field "word1" and have a script turn the string into an array and autofill text field "word1a" with the letter "c", "word1b" with the letter "h", etc..

Is this possible with the javascript installed in acrobat?  Would the file built in Acrobat DC be able to be filled in by someone else on another computer with only Reader.

I know this is possible with javascript, just not sure how to make it work with Acrobat.

What I have been trying is (which does not work):

var w1=this.getField('word1');

var w1a=this.getField('word1a');

w1a.value=w1.charAt(0);

I have also tried to convert the string into an array, but have not had any luck.  Is this something that would be done in the Document Javascripts or can you do it in the "custom calculations scripts" for the text field.

I have been able type each letter in a text field and add them together to form the word, so that is a work around I I can not get the javascript to work to split the word apart.

TOPICS
Acrobat SDK and JavaScript , Windows
4.4K
Translate
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 , Jan 25, 2019 Jan 25, 2019

the "getField()" function returns the field object. You want the value of the field.  This is a very common mistake.

var strWord4 = this.getField('strW4').value;

But you have other issues. The split function returns an array. Arrays values are accessed with the square brackets.

w4a.value=word4[0]; 

But, you don't need to split the string to get the first character. just use this

w4a.value = strWord4[0];

And what is "w4a" ? is it is field object? 

Translate
Community Expert ,
Jan 25, 2019 Jan 25, 2019

Are you looking for a comb? Acrobat can do that for you automatically. In the field properties, just select the "comb of" property and put in the maximum length. You don't need separate fields, Acrobat will space them out for you.

fo02.png

Translate
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
New Here ,
Jan 25, 2019 Jan 25, 2019

I don't think that is what I am looking for.  What I am wanting to do is split a word that a person types into a text field into its individual letter later on in the document.  A way to make a puzzle to unscramble the letters.

I can do it in the reverse order with the following script in the "Custom Calculations Script" for the text field.

var word2=this.getField('w2');

word2.value=this.getField('w2a').value+this.getField('w2b').value+this.getField('w2c').value+this.getField('w2d').value+this.getField('w2e').value+this.getField('w2f').value+this.getField('w2g').value+this.getField('w2h').value+this.getField('w2i').value+this.getField('w2j').value+this.getField('w2k').value+this.getField('w2l').value+this.getField('w2m').value+this.getField('w2n').value

I would be able to address each letter of the word individually this way, it is just a little bit more difficult tab between each text field to enter the letters individually then it would be just to type the word in and let the program break out each letter of the string.

Translate
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 ,
Jan 25, 2019 Jan 25, 2019

See this article:

https://acrobatusers.com/tutorials/splitting-and-rebuilding-strings

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

Translate
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
New Here ,
Jan 25, 2019 Jan 25, 2019

There is obviously something that I am missing.  I understand the javascript and have tried that. 

This is what I typed in for the code:

var strWord4 = this.getField('strW4')

var word4 = strWord4.split("");

w4a.value=word4(0).value

This is what the javascript debugger is telling me.

TypeError: strWord4.split is not a function

Translate
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 ,
Jan 25, 2019 Jan 25, 2019

the "getField()" function returns the field object. You want the value of the field.  This is a very common mistake.

var strWord4 = this.getField('strW4').value;

But you have other issues. The split function returns an array. Arrays values are accessed with the square brackets.

w4a.value=word4[0]; 

But, you don't need to split the string to get the first character. just use this

w4a.value = strWord4[0];

And what is "w4a" ? is it is field object? 

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

Translate
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
New Here ,
Jan 26, 2019 Jan 26, 2019

"w4a" is a form object field I want to write the first character of the array to. "w4b" is going to be the second form field i write the second character to.

I got it to work.  Thank you so much for your help.

var strWord4=this.getField('w4').value

var W4a=this.getField('w4a')

W4a.value=strWord4[0]

var W4b=this.getField('w4b')

W4b.value=strWord4[1]

var W4c=this.getField('w4c')

W4c.value=strWord4[2]

var W4d=this.getField('w4d')

W4d.value=strWord4[3]

var W4e=this.getField('w4e')

W4e.value=strWord4[4]

var W4f=this.getField('w4f')

W4f.value=strWord4[5]

var W4g=this.getField('w4g')

W4g.value=strWord4[6]

var W4h=this.getField('w4h')

W4h.value=strWord4[7]

var W4i=this.getField('w4i')

W4i.value=strWord4[8]

var W4j=this.getField('w4j')

W4j.value=strWord4[9]

var W4k=this.getField('w4k')

W4k.value=strWord4[10]

var W4l=this.getField('w4l')

W4l.value=strWord4[11]

var W4m=this.getField('w4m')

W4m.value=strWord4[12]

var W4n=this.getField('w4n')

W4n.value=strWord4[13]

The question I have now is is there a way to make the later form fields (such as w4m and w4n) not show up any value if the word length from the parent field "w4" is less than 14 characters long.  I have not tried to research this yet, since it is a new problem.  I just thought I would ask since you have been so helpful on the above issue.  Thank you again.

Translate
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 ,
Jan 26, 2019 Jan 26, 2019

W4n.value = (strWord.length < 14)?strWord4[13]:"";

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

Translate
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
New Here ,
Jan 26, 2019 Jan 26, 2019
LATEST

That worked great, thank you.

I had to adjust it a little:

W4n.value = (strWord4.length >= 14)?strWord4[13]:" ";

Translate
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