Copy link to clipboard
Copied
First of all forgive my English, it is not my native language. I'm having a problem with automatically filling out a form field from a drop-down list.
I have a drop-down list named "Options" and form fields named "name", "age" and "text".
I would like that from the option chosen in my drop-down list the name form field "text" would be filled with a predefined text using the values contained in the fields "name" and "age".
I managed to do this, however when I replace the values in the "name" and "age" fields and change the option in my drop-down list, the text in the "text" field is not updated and always uses the information from the previous choice.
I am not a programmer, just a curious person trying to improve processes in my job. Here's the step-by-step of what I did:
1st. I formatted the drop-down list so that when no option is selected, the "text" field is blank.
if( event.willCommit ) {
if(event.value == "") this.resetForm(["text"]); else
textscript(event.value);
}2nd. I created a script so that when I make a choice from the drop-down list, the "text" field is filled with the script's information.
// Placing all pre-population data in a single data structure
var textscripting = { "Option 1":{ story: "The funny " + this.getField("name").value + ", " + this.getField("age").value + ", was at Mom's house."},
"Option 2":{ story: "Mischievous " + this.getField("name").value + ", " + this.getField("age").value + ", went to Grandma's."},
"Option 3":{ story: "Ricky " + this.getField("name").value + ", " + this.getField("age").value + ", went to Daddy's house."},
};
function textscript(cCharactherName) {
// Filling in fields with values of the previously defined objects
this.getField("text").value = textscripting[cCharactherName].story;
}
So the intention is that when I choose option 1, the field "name" is "John", the field "age" is "20", the field "text" will bring me: "The funny John, 20, was at Mom's house."
It turns out that when I change John's name to Doc, for example, the options don't have Doc's name in their texts, but they still use John's name.
I would be immensely grateful if you can help me. I'm attaching the file so they can see more clearly what I wanted to say. Greetings from Brazil.
Copy link to clipboard
Copied
Yes, you're welcome. Just to be clear I am not a programmer either. I follow some developers in these forums and learn from their guidance.
If you want my script to behave like the document-level script all you have to do is remove this line:
event.value =" ";
That was my mistake. I had it there because I was unsure if this is what you wanted.
When you remove that entry , this script will behave in the same way as Thom Parker's document-level script; it will change once you select a different option from the combobox.
NOTE: I am still figuring out why the original scrfipt breaks. I will post back again.
Copy link to clipboard
Copied
Hi,
If the script is not working wher did you place the funtion ? Is it a document level script?
Where are you placing these scripts?
The other thing is that I noticed that you may need to use this line instead event.value =""; (I least I prefer to use this option instead of the resetForm() method).
If you use the resetForm() method you must manually set the default value in the drop down menu (like an empty space) .
When you 're editing the dropdown fields select the empty line entry and close the field properties dialogue box. The last value that you select here before you close the field porperties dialogue box will remain as the default value.
You also need to un-tick the box that allows users to enter custom text, and also check the box " Commit selected value immediately".
See slide:
And is this part a custom key stroke script?
if( event.willCommit ) {
if(event.value == "") this.resetForm(["text"]); else
textscript(event.value);
}
I think that it should be phrased to something like this:
if(event.willCommit ) {
if(event.value != "") {
// call for the function here...
} else {
this.resetForm(["text"]);
/// or use event.value ="";
}
}
But my main question is : where did you get this script from ? What is the reference?
Copy link to clipboard
Copied
Hello! First of all thanks for the help.
Answering your question:
And is this part a custom key stroke script?
Yes, this is.
The second script is at the document level.
I liked your idea to reset the "text" field when there is no option chosen from the drop-down list. But my problem is to form a text from a choice in the drop-down list. The text will use the information contained in other form fields, in which case it will be the form fields "name" and "age".
where did you get this script from ? What is the reference?
The script was based on this one >> https://acrobatusers.com/tutorials/change_another_field/ . It works if we want to fill a form field with pre-defined text in a document-level script based on a choice made from a drop-down list. But when I insert this.getField ("xxx") in this predefined text, it only works for the first choice made in the drop-down list. When I make another choice, the field containing the text does not update with the new value of this.getField ("xxx").
Copy link to clipboard
Copied
Hello Addison,
I apologize for taking this long to reply.
I want to acknowledge that the script you obtained from the Thom Parker's tutorial ( how to change another field) is excellent. But if you've noticed how the document-level script was constructed into a function that will be called from a keystroke event in a combobox, it doesn't combine fieldname values with text strings.
The way you've set it up is just breaking the script continuously and throwing many errors.
How the value of the textfield is committed or not will depend on how to get the current value of any selections made by the user in the combox.
I tried working around it for too long and I know someone with more experience can throw the piece that we're missing with something (perhaps) along the lines of "event.change = event.changeEx"
On the other hand, for the type of task that you're asking about I really really don't think that a document-level function is needed (unless your project requires so and is a lot more complex).
In any case, I was able to get the same results and just a little more flexible with a much smaller custom calulation script>
It can be run directly from the "text" field and there's no need to invoke keystrokes nor mouse-up actions of anykind.
Here's what I did:
var Option1 = "The funny " + this.getField("name").value + ", " + this.getField("age").value + ", was at Mom's house.";
var Option2 = "Mischievous " + this.getField("name").value + ", " + this.getField("age").value + ", went to Grandma's.";
var Option3 = "Ricky " + this.getField("name").value + ", " + this.getField("age").value + ", went to Daddy's house.";
var f = this.getField("Options").value;
if ((this.getField("name").value !=="") || ( this.getField("age").value !=="")) {
if ((f ==" ") || (f =="")) event.value = "";
else if (f == "Option1") event.value = Option1;
else if (f == "Option2") event.value = Option2;
else if (f == "Option3") event.value = Option3;
}
This script achieves exactly the same result of the document-level script, but it also allows you to change the name and the age in realtime without having to make a selection in the combobox each time. It will be displayed immediately in the "text" field.
Here's the sample PDF so you can see it in action and improve the code:
Please let me know if this solution met your expectations
Copy link to clipboard
Copied
I forgot to add that this is even a way better version of the same script above, since it clears all the fields ("name", "age", and "text") when a null ("") selection is made in the combobox:
var Option1 = "The funny " + this.getField("name").value + ", " + this.getField("age").value + ", was at Mom's house.";
var Option2 = "Mischievous " + this.getField("name").value + ", " + this.getField("age").value + ", went to Grandma's.";
var Option3 = "Ricky " + this.getField("name").value + ", " + this.getField("age").value + ", went to Daddy's house.";
var f = this.getField("Options").value;
event.value =" ";
if (event.source && event.source.name=="Options") {
if ((this.getField("age").value !=="") && ( this.getField("name").value !=="")) {
if (f == "Option1") event.value = Option1;
else if (f == "Option2") event.value = Option2;
else if (f == "Option3") event.value = Option3;
else if ((f.value != "Option1") || (f.value !="Option2") || (f.value !="Option3") ) {this.getField("name").value=""; this.getField("age ").value=""; }
}
}
Copy link to clipboard
Copied
Hello, ls_rbls!
Thank you very much for your cooperation, it really works. I had thought of something like this, however there is a catch: this way there is no way for us to edit the "text" form field after a choice made in the drop-down list. The final intention of this form that I am preparing is that the final text is a medical report and has a part that is standard and I want to leave the field cleared for future editions. Thom Parker's script allows me to do just that, make edits after a choice is made in the drop-down list. I really think there is some code that we could use so that the document-level script is updated with each choice we make in the drop-down list. Unfortunately, my programming knowledge doesn't allow me to go any further, despite a lot of research done.
Thanks again for your time and collaboration.
I will use your script for another purpose, with a text that does not need to be edited later.
Copy link to clipboard
Copied
Yes, you're welcome. Just to be clear I am not a programmer either. I follow some developers in these forums and learn from their guidance.
If you want my script to behave like the document-level script all you have to do is remove this line:
event.value =" ";
That was my mistake. I had it there because I was unsure if this is what you wanted.
When you remove that entry , this script will behave in the same way as Thom Parker's document-level script; it will change once you select a different option from the combobox.
NOTE: I am still figuring out why the original scrfipt breaks. I will post back again.
Copy link to clipboard
Copied
Very good! Definitely removing the part you indicated solves my problem and I believe that the problem of many people! Thank you very much!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now