Dynamic text from a drop-down list does not update.
- August 14, 2020
- 1 reply
- 3304 views
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.
