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

JavaScript for refreshing fields?

New Here ,
Jan 17, 2016 Jan 17, 2016

Hello. I have a form with question fields. I would like to populate several other fields with the answer. The question's answer text field is named "text1" and whatever text is filled in "text1" will also populate in "text2" thru "text8" fields. I found the code below in this forum and modified it to fill in all seven other locations. I placed this code in 'Text Field Properties', 'Calculate' tab, 'Custom calculation script:'.

All works well if all the fields are in the same PDF... I extracted the question page as a separate PDF with the "text1" field (Let's call this the "question" PDF) and kept all the other fields in the "answer" PDF. If I placed a test answer in the "text1" field, saved, then inserted that page into the "answer" PDF the other seven text fields do not get the value of "text1" unless I retype in "text1" field or backspace a few characters and retype then hit enter.

Is there JavaScript that I can place on a button that will refresh all the text fields so I will not need to retype? (the "question" PDF will have close to 50 questions so that would be a hassle). Thank you.

// Populate the other field with this field's value

getField("text2").value = event.value;

getField("text3").value = event.value;

getField("text4").value = event.value;

getField("text5").value = event.value;

getField("text6").value = event.value;

getField("text7").value = event.value;

getField("text8").value = event.value;

TOPICS
Acrobat SDK and JavaScript , Windows
4.6K
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
LEGEND ,
Jan 17, 2016 Jan 17, 2016

So it appears you want Acrobat's JavaScript to detect the missing field and fix the name. Well it is possible for JavaScript to trap the error of the missing field.

Normally JavaScript stops for the entire form when a critical JavaScript error is found. One can use the "try...catch" block of code to trap the error or test the result of the getField method result for its documented return value.

I have posted numerous times a function that uses a field name as an input parameter and returns the field object for found fields and a null value if no field with the provided name was not found. The function also issues an alert about the missing field. Because the error has been identified for the field the scripts will continue to run and sets the expected field object to a null value. One can test for this null value and when found branch around the script trying to access this missing field.

It could be possible to use JavaScript to read each field name in the document and try to figure which existing field was intended to be used.

I would use the On Blur action and not the calculation action, since the On Blur action would only execute when the text1 field is updated. Using the calculate option will trigger the calculation script for Text1 each time any field is used in a calculation.

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 18, 2016 Jan 18, 2016

Hello GKaiseril. I should have mentioned in my post that I am a novice when it comes to JavaScript. I'm not quite sure what you mean by "missing field". All the fields are named properly and in the document. I am guessing that you are referring to the fact that when I insert the PDF page with the "text1" field into the PDF with the "text2" thru "text8" that JavaScript does not find "text2" thru "text8" until I retype some characters in the "text1" field.

I will try moving the JavaScript to the "Actions" section and use the On Blur. I will report back this evening.

Thank you!

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
LEGEND ,
Jan 18, 2016 Jan 18, 2016

The script should not be a custom calculation script for text1, but rather a custom validate script. It should work as you want after you make this change.

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 Beginner ,
Jan 21, 2016 Jan 21, 2016

attach doclevel script with one line

calculateNow()

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 28, 2016 Jan 28, 2016

Hello lev.savranskiy. Where should I place that calcualteNow() script?

Thank you!

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 Beginner ,
Jan 28, 2016 Jan 28, 2016

doclevel  as i said

temp2.jpg

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 28, 2016 Jan 28, 2016

Thank you for the reply lev.savranskiy.

As I am a novice with setting up scripting in a form, you have been very helpful with sending a screen shot to simplify it for me. I will do as you suggest this evening and report back later.

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
New Here ,
Jan 28, 2016 Jan 28, 2016

Hello again lev.savranskiy.

I just found out that since I have Acrobat XI standard, I do not have the document level javaScript. Can you recommend a solution for me to add this capability?

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 28, 2016 Jan 28, 2016

You can use document level scripts even with Acrobat Standard, it's just a bit more complicated to get them into your document: Document Level Scripts With Acrobat Standard - KHKonsulting LLC

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
LEGEND ,
Jan 28, 2016 Jan 28, 2016

One can also add document level scripts by inserting a PDF with the document level script and then deleting the inserted pages. The document level script will remain.

One can even add the function that traps fields not found by the calculation script by including it the calculation.

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 28, 2016 Jan 28, 2016

Thank you gkaiseril. It sounds like from what you are saying, if I was to use khkremer's tool for adding document level scripts, that other folks who use the form I create will still be loading the document level script without needing to install the tool themselves. I like the suggestion of just inserting a page with the doc level script then deleting it. I will ask some others in my group if they have Pro version.

This thread has become somewhat diluted (or just my focus has). I will start back from the top and experiment with a few items to see which one works. Too bad I can't just insert a button that would "reload" or "refresh" the javaScript for all form field items. That would be the most user freindly.

I will mark "Correct Answer" in this thread as soon as I finish the experiments. Feel free to keep posting ideas.

Thank you all.

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 Beginner ,
Jan 29, 2016 Jan 29, 2016

you can put script on folder level if you wish.C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Javascripts

but all users have to do the same later

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
LEGEND ,
Jan 29, 2016 Jan 29, 2016

The script provided in this discussion might have an issue by not providing the value for a document object or using the "this" value for the document object.

You folder reference is for Acrobat XI. The path for the folder level scripts was changed with version 10.01. I am sure there are many users still using version 8 and 9 and possibly even older.

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
LEGEND ,
Jan 29, 2016 Jan 29, 2016
LATEST

This is one of many ways to add a document level script. th

One can even create an FDF file to insert document level scripts into a PDF with Acrobat. It is even possible to add a folder level script that can add a button and script to Reader to import an FDF file into Reader and use that new tool to add a document level script with Reader.

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