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

Need help scripting text field dependant on drop down menu but also auto-populate

Explorer ,
Oct 14, 2024 Oct 14, 2024

I need help creating/scripting a text field that's dependent on a drop down menu, but also having the text field auto-populate to another text field

 

I'm really not great at scripting, and I apologise in advance I'm really dense when it comes the coding terminology, lol

 

but I've managed to workout 90% of what I need the Custom Calculate Script to do,

 

This is what I have so far:

 

if (event.value == "Option1")

{

var field1Value = this.getField("index1").value;

this.getField("text combo").value = field1Value;

}

 

 

If I type text into the “index1” field, it works good and updates the “text combo” field,

but not the other way around

 

I know if I want it to work the other way around then just swap “text combo” with “index1”

 

but I need them to both work and I don’t know how to make the script to be able to do both at the same time

 

I tried adding both of these scripts into the “Run custom Calculate script” box like this, but I couldn’t get them both to work together 😬

 

if (event.value == "Option1")

{

var field1Value = this.getField("index1").value;

this.getField("text combo").value = field1Value;

}

{

var field1Value = this.getField("text combo").value;

this.getField("index1).value = field1Value;

}

 

anyone have any ideas?

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF , PDF forms
3.3K
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
1 ACCEPTED SOLUTION
Community Expert ,
Oct 18, 2024 Oct 18, 2024

Hi there @kenth_3409 !

 

I just wanted to add my ten cents to this discussion.

 

Use the updated PDF that I am linking here for you and confirm if it works as intended : Department Dropdown

 

I've only used Mouse Up event JavaScript scripts and Custom Keystroke scripts. For example, the "index1" textfield has a Mouse Up event with the following script:

 

this.getField("Combo Box1").currentValueIndices = [1];

 

And also the following Custom Keystroke Script:

 

if(event.willCommit) this.getField("text combo").value = event.value;

 

 

Respectively, the "index2" textfiled is using a Mouse Up event with:

 

this.getField("Combo Box1").currentValueIndices = [2];

 

 And using the same Custom Keystroke Script:

 

if(event.willCommit) this.getField("text combo").value = event.value;

 

 

The intention is for index1 or index2 fields to execute a script as soon as the user starts typing in a value.

 

Such Mouse Up event script will pre-select the desired Face Value from the "Combo Box1" dropdown menu, and it will populate the "text combo" field (with the value that was typed in on those index fields),  as soon as the user hits enter.

 

Doing so, it saves time for the user; they don't need to manually drag and hover the mouse pointer over the "Combo Box1" dropdown menu to expand the list and actually select an option by click on it. 

 

In contrast to the solution offered above, the following Custom Keystroke Script is used in the "text combo" field:

 

if(event.willCommit && (this.getField("Combo Box1").value == "Option1")) {

this.getField("index1").value = event.value;
}

else if(event.willCommit && (this.getField("Combo Box1").value == "Option2")) {

this.getField("index2").value = event.value;
}

 

 

For instance, if "Combo Box1" already has an option selected (Option1 or Option2), typing in any value on this field will auto-populate its data on the corresponding index field.

 

If a user desires to switch (or perhaps, they change their mind), all they have to do is to manually select Option1 or Option2 from the dropdown menu ("Combo Box1").

 

I hope this meets your requirements. Let us know if it helped.

 

SOME REFERENCES:

 

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html

 

https://acrobatusers.com/tutorials/change_another_field/

 

https://community.adobe.com/t5/acrobat-discussions/how-do-i-show-the-selected-item-name-and-not-the-...

 

 

View solution in original post

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

Your description doesn't make much sense, I'm afraid. You want to copy field1 to field2 and then field2 to field1? What's the point of the latter action? The first one already guarantees they will have the same value. The second action doesn't do anything.

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
Explorer ,
Oct 14, 2024 Oct 14, 2024

Hi try67!

 

Thanks for your reply 🙂

 

I need the text fields to auto update.

I know I can do this easily if I have multiple text fields with the same property name,

then when you type text in one, all the other text fields with the same property name will also update with whatever is typed in…

 

but I need the text field called “text combo” to also be dependent on the dropdown/combo box menu (option1, option2, etc)

 

so I used the first script I mentioned in my post.

 

 

when I type in the text field called “index1” it updates the dependent text field called “text combo” when “option1” is selected

which is what I want,

 

but I also need the text field called “index1” to update when I type in the text field called “text combo” when “option1” is selected from the dropdown menu.

 

Sorry, I’m bad at explaining things, lol

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

I'm sorry, I don't follow. Your last two sentences ("when I type..." and "but I also...") are describing the same thing, as far as I can see.

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
Explorer ,
Oct 15, 2024 Oct 15, 2024

I guess what I meant in my last sentence is that maybe in theory the text field called “index1” should autofill when I type in the text field called “text combo” and “option1” is selected,

but it doesn’t

I’m not sure why

 

It just keeps reverting back to whatever I typed in the text field called “index1” after I type something in the field called “text combo”

 

so I thought maybe something was wrong or missing in the script I used, or maybe it’s a glitch?

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

What do you mean by "both" and "other way around"?  If the dropdown equals "Option1", you want the "text combo" field to equal the "index1" field and that is working.  Now what?

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
Explorer ,
Oct 14, 2024 Oct 14, 2024

Hi PDF Automation Station!

 

Thanks for your reply 🙂

 

I need the text fields to auto update.

I know I can do this easily if I have multiple text fields with the same property name,

then when you type text in one, all the other text fields with the same property name will also update with whatever is typed in…

 

but I need the text field called “text combo” to also be dependent on the dropdown/combo box menu (option1, option2, etc)

 

so I used the first script I mentioned in my post.

 

when I type in the text field called “index1” it updates the dependent text field called “text combo” when “option1” is selected

which is what I want,

 

but I also need the text field called “index1” to update when I type in the text field called “text combo” when “option1” is selected from the dropdown menu.

 

Sorry, I’m bad at explaining things, lol

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

I sounds like what you want it going to create an infinite loop - the only way out of which is to do a hard shut down of Adobe.  Can you upload a form with only the affected fields and describe exactly what you want to happen?

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
Explorer ,
Oct 15, 2024 Oct 15, 2024

I made the form and described it the best I can, hopefully it makes sense 😅

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

Remove all your scripts and enter the following custom validation script into field index1:

if(this.getField("Combo Box1").value=="Option1")
{
this.getField("text combo").value=event.value;
}

 

Enter the following custom validation script into field index2:

 

if(this.getField("Combo Box1").value=="Option2")
{
this.getField("text combo").value=event.value;
}

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
Explorer ,
Oct 16, 2024 Oct 16, 2024

I tried what you suggested and used the script you gave me into the custom validation script for text field called “index1”, and also the script you gave me for the text field called “index2”

 

but when I typed into the text fields called “index1” and “index2” nothing updated at all,

 

I also tried using the scripts as a custom calculation script,

and it worked like the script I originally tried where it updated the field named “text combo” when I typed in index1 or index2 and the linked option was selected,

but it didn’t update when I typed anything into the text field named “text combo”

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

You can't use a calculation script or you won't be able to modify "text combo".  I tested this and it works.  Check the did you copy and paste my script.  Is your dropdown named "Combo Box1" exactly, including case sensitivity?  Do your dropdown values match the script exactly, including case sensitivity?  Did you check the console for errors?  Did you tab out of the text field after entering the value?

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
Explorer ,
Oct 16, 2024 Oct 16, 2024

I copied and pasted the script you provided and followed your instructions to put it in the custom validation script, I checked for case sensitivity and tabbed out of the text field after entering the value, but there still some problems with it.

 

The text in text combo only updates if I type in the text field called “index1” or “index2” AND have the linked option selected,

 

and when I type in the text field called “text combo” then nothing updates at all

 

 

I’m not sure if I did the debugging thing right, but it said syntax error

 

I updated the PDF after following your instructions and uploaded it as an attachment and also a screen cap of the debugger thing.

 

I’m not sure what I’m doing wrong 😥

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

In the screenshot of your explanation, it is doing exactly what I programmed the script to do, so it looks like I misunderstood your instructions.  Please, field by field, explain exactly what you want to happen when the field is interactive with.  Use the field names in your description.  Also state which parts, if any, are working the way they are supposed to.

PDFAutomationStation_0-1729158588079.pngexpand image

 

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

You need to use scripts under  'Validate' tab, not 'Calculate'.

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
Explorer ,
Oct 16, 2024 Oct 16, 2024

Hi Nesa Nurani 🙂

I tried that, it didn't work 😞 

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

Hi there @kenth_3409 !

 

I just wanted to add my ten cents to this discussion.

 

Use the updated PDF that I am linking here for you and confirm if it works as intended : Department Dropdown

 

I've only used Mouse Up event JavaScript scripts and Custom Keystroke scripts. For example, the "index1" textfield has a Mouse Up event with the following script:

 

this.getField("Combo Box1").currentValueIndices = [1];

 

And also the following Custom Keystroke Script:

 

if(event.willCommit) this.getField("text combo").value = event.value;

 

 

Respectively, the "index2" textfiled is using a Mouse Up event with:

 

this.getField("Combo Box1").currentValueIndices = [2];

 

 And using the same Custom Keystroke Script:

 

if(event.willCommit) this.getField("text combo").value = event.value;

 

 

The intention is for index1 or index2 fields to execute a script as soon as the user starts typing in a value.

 

Such Mouse Up event script will pre-select the desired Face Value from the "Combo Box1" dropdown menu, and it will populate the "text combo" field (with the value that was typed in on those index fields),  as soon as the user hits enter.

 

Doing so, it saves time for the user; they don't need to manually drag and hover the mouse pointer over the "Combo Box1" dropdown menu to expand the list and actually select an option by click on it. 

 

In contrast to the solution offered above, the following Custom Keystroke Script is used in the "text combo" field:

 

if(event.willCommit && (this.getField("Combo Box1").value == "Option1")) {

this.getField("index1").value = event.value;
}

else if(event.willCommit && (this.getField("Combo Box1").value == "Option2")) {

this.getField("index2").value = event.value;
}

 

 

For instance, if "Combo Box1" already has an option selected (Option1 or Option2), typing in any value on this field will auto-populate its data on the corresponding index field.

 

If a user desires to switch (or perhaps, they change their mind), all they have to do is to manually select Option1 or Option2 from the dropdown menu ("Combo Box1").

 

I hope this meets your requirements. Let us know if it helped.

 

SOME REFERENCES:

 

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html

 

https://acrobatusers.com/tutorials/change_another_field/

 

https://community.adobe.com/t5/acrobat-discussions/how-do-i-show-the-selected-item-name-and-not-the-...

 

 

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

Now I finally understand what @kenth_3409 want these fields to do.  Good work.  IMO, you shouldn't use a Mouse Up action in  a text fields to change the combo box selection.  What if the user tabs into the field?  Move those scripts to On Focus actions and they will cover both scenarios (mouse click and tab into).

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
Explorer ,
Oct 18, 2024 Oct 18, 2024

@PDF Automation Station

Is_rbls solved my question, but I still wanted to say thank you for you patience and willingness to help.

I really appreciate it and it means a lot 🙂

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

@PDF Automation Station ,

 

I like that yes, I thought of it but I wasn't sure.

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
Explorer ,
Oct 18, 2024 Oct 18, 2024

Hi Is_rbls 🙂

 

Yes! This is exactly what I wanted it to do! thank you so much 😄

 

One more quick question though, but how can I make the text field called “text combo” say “-Select-” when the -Select- option is chosen in Combo Box1?

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

You may add a Custom Keystroke script to "Combo Box1" like so:

 

 

if(event.willCommit && (event.value == "-Select-")) {
this.getField("text combo").value = event.value
}

 

 

And modify the Custom Keystroke script in the "text combo" field as:

 

if (event.willCommit && (event.value == "-Select-")){
this.getField("index1").value ="";
this.getField("index2").value ="";
 } 

else if (event.willCommit && (event.value !== "-Select-")){

if(event.willCommit && (this.getField("Combo Box1").value == "Option1")) {
this.getField("index1").value = event.value;
 }

if(event.willCommit && (this.getField("Combo Box1").value == "Option2")) {
this.getField("index2").value = event.value;
 }
}

 

 

I am sure there are many other ways to accomplish this in a more elegant and concise fashion using Acrobat JavaScript.

 

But given the arcane nature (and somewhat limited features) of some PDF objects such as, combo boxes or list boxes, and more specifically when they're used in calculations that involve other dependent field objects (such as  text fields),  I had to creatively employ Custom Keystroke scripts to work around the barriers of infinite loops that trigger with other conventional approaches (as @PDF Automation Station warned us earlier).

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 ,
Oct 18, 2024 Oct 18, 2024
LATEST

You're very welcome.

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