+++ EDITED REPLY - CORRECTED TYPO IN THE SCRIPT
this line : var selectedName = event.value; is missing the full variable declaration
it should read var f4 = selectedName = event.value;
++ Adding to the topic,
Hi @ShifftyGuy ,
It looks like you might be rushing a bit; if I'm not mistaken, the part that turns the wheels and supports your vehicle's weight is called an 'axle,' not 'axel.'
I'm mentioning this not to criticize your spelling in your code, but to emphasize how crucial it is to use the correct syntax in programming.
Each programming language has its own rules for building expressions, which are made up of operators, constants, and variables. These can include one or more operands or operators to produce a value. Every punctuation mark, symbol, or operand has a specific role in programming.
In the case of Acrobat JavaScript, especially concerning your objective, you need to write a script that uses a Mouse Event action to fill in values in specific text field objects.
The solution that @Nesa Nurani shared, which is also found in the script that @Tariq Ahmad provided have particular syntax that seems to be missing in your version.
For example, you should look into the errors that pop up when you run the script in the console. By pressing 'CTRL' + 'J', you can open the JavaScript console to check for any errors. You will probably see that the first error message says something like 'Red is not defined.'
So, if you compare your copied script with Tariq's, the JavaScript console is telling you that you missed the necessary quotes in the object literals that should be defined in the variable "cList"
See how you expressed it below:
"Vehicle 1": { color: Red, axel: 2, weight: 2050 },
The object literals such as Red, 2 and 2050 must be in between quotes (""), like this:
"Vehicle 1": { color: "Red", axel: "2", weight: "2050" },
Also, since you are executing the Mouse Event script from the event target field itself (the dropdown menu field object) , you must express variable 'selectedName" as event.target.value instead of 'event.value' :
this line right here ===>> var selectedName = event.value;
change to ===== >>> var selectedName = event.target.value
NOTE: or you can express that variable selectedName using the full name of the dropdown field as shown in Tariq's script like so:
change to ===== >>> var selectedName = this.getField("Name of the dropdwon menu here").value
Now, take a Look at these lines below :
f3.value = cList[selectedName].axel;} <<< ===== you don't need curly braces here f4.value = cList[selectedName].weight;} else {
That said, I've noticed that Vehicle 1 and Vehicle 2 have the same weight , 2050? Is this correct? As for Vehicle 3, it has 3 axles... is this also correct? (just want to make sure).
In any case, Below is the refined version of your script with corrections made, leaving the word "axel" spelled as is in your current script:
var cList = {
"Vehicle 1": { color: "Red", axel: "2", weight: "2050" },
"Vehicle 2": { color: "Yellow", axel: "2", weight: "2050" },
"Vehicle 3": { color: "Black", axel: "3", weight: "7452" }
};
var f1 = this.getField("color");
var f2 = this.getField("axel");
var f3 = this.getField("weight");
var f4 = selectedName = event.target.value;
if (cList[selectedName]) {
f1.value = selectedName;
f2.value = cList[selectedName].color;
f3.value = cList[selectedName].axel;
f4.value = cList[selectedName].weight;
} else {
f1.value = "";
f2.value = "";
f3.value = "";
}
... View more