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

Set the default field to the value of a previous variable

Explorer ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Hi there,

Is it possible to use a variable from a previous form as the default value (only) in another form?

For example, a previous form has the name 'totalhours' and calculates a total value of 6.25.

In my next form I would like to reference that value as the default value in a new text field, but allow the user to enter a new value independent of the previous variable 'totalhours'.

I see other posts where it is possible to set the defaultvalue, but I am unable to work out the javascript to my particular circumstance. In other words, I would like the default value for my new field 'desiredhours' to be generated from 'totalhours' merely as a default value.
If that's not clear enough, let me add that if I enter a new value in 'desiredhours' it will be a new value and not affect the original 'totalhours' it merely uses the value as a display start point.

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF forms

Views

196

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 , Sep 30, 2024 Sep 30, 2024

No, but you can do it using a simple calculation script:

 

if (event.value=="") event.value = this.getField("totalhours").value;

Votes

Translate

Translate
Community Expert ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

When you say "default value" do you mean initial value that can be overwritten, or do you mean default value that it will return to when the field is reset?  Do you want these forms connected so that 6.25 (in this example) is automatically used as the default value, or are you wanting to manually set it in form #2?

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
Explorer ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Yes, I mean the default value that can be overwritten. I do NOT want the forms connected (such as totalhours#1)
I would like the new form to set a unique new field value, but use the calculation from the previous form as a starting point.

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
Explorer ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

form-idea-01.jpgAs an aside - it would be great if I could simply reference a field name in the 'default value' dialog box of options. (With no javascript required)
For example, I can manually enter any number or text as a 'default value' but if I could enter a variable reference such as #totalhours or $totalhours in this box, that would be superb. (I'm assuming that's not a thing?!)

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 ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

No, but you can do it using a simple calculation script:

 

if (event.value=="") event.value = this.getField("totalhours").value;

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
Explorer ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Fantastic! It displays the right value, but for some reason, it works differently from a 'default value' because the other fields that use this field for calculation still assume a value of nil? (Until I type something in)
Is there a way to access/apply the same 'default value' variable?

 

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 ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Check your calculation order.

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 29, 2024 Oct 29, 2024

Copy link to clipboard

Copied

In my initial response I asked "do you want these forms connected?".  You responded "I do NOT want the forms connected (such as totalhours#1) I would like the new form to set a unique new field value, but use the calculation from the previous form as a starting point."  Isn't this a contradiction?

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 29, 2024 Oct 29, 2024

Copy link to clipboard

Copied

I think you are misunderstanding default value.  If you reset the field using a reset button, the value will revert to whatever is in the default value field of the options tab (usually nothing).  When you first enter a value in this field and close the properties the default value will display as the value.  @try67 's script will display the totalhours field value when no value has been entered.  Once a value has been entered, that entry will be the value.  If that's not what you want, please clarify.  If you want the field to display totalhours, but have value of null, you would move @try67  's script to a custom format script instead of a custom calculation script.

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 29, 2024 Oct 29, 2024

Copy link to clipboard

Copied

LATEST

"Is there a way to access/apply the same default value variable?"  Yes:

this.getField("TheField").defaultValue //for another field

event.target.defaultValue // for the field the script is in

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