Copy link to clipboard
Copied
Hello Gents,
I'm looking for a javascript that will help me populate, a chosen option from a dropdown menu. The principle is that when I choose option YES the few dropdown boxes on the other pages are blank and I'm able to choose from 3 options YES, NO and N/A.
I've tried calculation script event.value = this.getField("Dropdown1").value; in those boxes to "read" first dropdown menu but there is a problem with that. When I choose YES it will populate that selection to other boxes and it won't let me change it. I want it to work so when I choose N\A it copies N\A and when I choose YES it will let me select if the answer to the question is YES or NO.
Thanks in advance for any sugestions.
Krystian
Copy link to clipboard
Copied
Excellent news! You look at a problem long enough and thinks become clearer.
I have a couple of comments.
1) It is not necessary to clear and set the drop down items. This adds a lot of code you'll need to debug and maintain. Instead, disable the dropdowns by setting the readonly property. Much simpler.
2) Handling the "Other" fields will be much easier if you use the group naming I mentioned in the previous post
Copy link to clipboard
Copied
You're request is not clear. Please try to explain what you want in simple clear terms.
However, it seems you are interested in changing another field from a dropdown.
Here is an article on that topic:
https://acrobatusers.com/tutorials/change_another_field
Copy link to clipboard
Copied
Hello Thom,
Thank you for quick reply.
I was bit unclear so let's start again.
My form has got one drop-down box on the first page with two options to choose: Yes and NA. This box let user to choose if that option exist or not.
If feature exist then user is choosing Yes. What should happen is that a dropdown fields in the document ( around 10 of them ) are open and blank. User can then choose one from 3 options: Yes, No and NA in those dropdown fields.
When feature is not available then users is choosing NA. What should happen after that is that NA is automatically copied in the same drop-down boxes explained above.
I hope that makes it clear a bit more.
One drop-down box controls what's happening with the other 10 drop-down boxes. NA or act as a normal box.
I've tried with the script mentioned above but it doesn't work as I want it.
Krystian
Copy link to clipboard
Copied
I'm sorry, but your explanation doesn't make any sense. But I think I get what you want.
You say that you want the first dropdown to control the behavior or the other 10 dropdowns.
if "Yes" is chosen, then the other dropdowns are set to a blank value and enabled so the user can select the value.
if "NA" is chose, then the other dropdowns are set to NA and disabled so the user cannot change the value.
All the action is driven from the first dropdown, so the script should be a custom validation script on the first dropdown.
if(event.value == "Yes")
{// Enable and blank other dropdowns
}
else if(event.value = "NA")
{// Disable and set to NA other dropdowns
}
You can make this task a lot easier by group naming the other 10 dropdowns, such as "Grp1.DropDown1" "Grp1.DropDown2", etc.
Copy link to clipboard
Copied
Hello Thom,
I've tried my best
Right, that's exactly what I meant but there are other drop-down boxes that should not be affected by option NA. So when they choose NA only those 10 dropdowns should be locked.
Thanks for your suggestions but I can't group those dropdowns as they are all different questions. So choosing Yes it will automatically answer all the questions.
It's some kid of quality control document and if feature on our equipment is not available, they ticking it as NA. Fields with questions regarding this feature should not be active.
I'm trying to speed up the whole process of filling the document.
What script would you suggest in that case? Maybe add something to those 10 boxes like. When NA is chosen then place NA and when Yes then just ignore it and leave it blank?
Thanks in advance
Krystian
Copy link to clipboard
Copied
Hello Thom,
I think I found something that will work.
var f = this.getField("dropdown2");
f.clearItems();
/* In a keystroke event, event.change contains the non-export value of
** the item selected by the user. */
switch (event.change) {
case "YES":
f.setItems(["", "YES", "NA", "NO"]);
break;
case "NA":
f.setItems(["NA"]);
}
}
I've added this script to first box under Format tab own keystroke script.
What do you think about it?
Regards,
Krystian
Copy link to clipboard
Copied
Got it!
if (!event.willCommit) {
var a = this.getField("204"); <--- Dropdown on other pages
var b = this.getField("206"); <-- Dropdown on other pages
a.clearItems();
b.clearItems();
switch (event.change) {
case "YES":
a.setItems(["", "YES", "NA", "NO"]);
b.setItems(["", "YES", "NA", "NO"]);
break;
case "NA":
a.setItems(["NA"]);
b.setItems(["NA"]); }
}
All I need to do now is add other combo boxes as c, d, e, ect.
Copy link to clipboard
Copied
Excellent news! You look at a problem long enough and thinks become clearer.
I have a couple of comments.
1) It is not necessary to clear and set the drop down items. This adds a lot of code you'll need to debug and maintain. Instead, disable the dropdowns by setting the readonly property. Much simpler.
2) Handling the "Other" fields will be much easier if you use the group naming I mentioned in the previous post
Copy link to clipboard
Copied
Hello Thom,
add 1)
Ok so I can delete the part of the code:
a.clearItems();
but how would I add that read only property bit?
add 2
I can't group those other dropdown boxes. The thing is that every box is a different answer so if I group them and choose answer in one of them it will automatically change answers in the others. Unless I'm missing something here..?
Krystian
Copy link to clipboard
Copied
On a PDF, form fields are grouped by prefixing the field name with a group name with the "dot" notation.
For example: "Grp1.Fld1" "Grp1.Fld2" "Grp1.Fld3"
These field names are all have the same group name of "Grp1". They are all unique individual fields, with their own value.
The advantage is that they can all be collected into an array so they can be operated on together.
var aFldArray = this.getField("Grp1").getArray();
This makes it easy to work on groups of fields.
Copy link to clipboard
Copied
Hello Thom,
I didn't know that you can group those combo boxes like that. You're right then, it will make the script much shorter as you only write it for one set of combo boxes.
Could you please help me modify that code so I fully understand how it should look like?
if (!event.willCommit) {
var a = this.getField("Dropdown2"); <--- Dropdown on other pages
var b = this.getField("Dropdown3"); <-- Dropdown on other pages
a.clearItems();
b.clearItems();
switch (event.change) {
case "YES":
a.setItems(["", "YES", "NA", "NO"]);
b.setItems(["", "YES", "NA", "NO"]);
break;
case "NA":
a.setItems(["NA"]);
b.setItems(["NA"]); }
}
I've also used a script to check if all fields are filled. It works ok as it shows all the pages that containt empty fields but I would like to add a message that all fields are ok.
Copy link to clipboard
Copied
If the fields are group named, then this code will do the same as your code above.
if(!event.willCommit)
this.getField("Grp1").getArray().forEach(function(a){a.setItems((event.change=="YES")?["", "YES", "NA", "NO"]:["NA"]);});
Create a new thread for your new question.
Copy link to clipboard
Copied
Hello Thom,
It worked like a charm and you were right, the code is like ten times shorter now.
Regarding the app alert part, I worked it out. I've just added
else
{ // all required fields contain data
app.alert("OK");
}
Thanks for your help one again.
Copy link to clipboard
Copied
Thom,
Is it possible to use your script to control two groups via one combo box like this:
Combo box Main : --> Option A --> Combox Box Group 1 - NA
--> Combox Box Group 1 - NA
--> Combox Box Group 1 - NA
--> Combox Box Group 2 - YES, NA, NO
--> Combox Box Group 2 - YES, NA, NO
--> Combox Box Group 2 - YES, NA, NO
Combo box Main : --> Option B --> Combox Box Group 1 - YES, NA, NO
--> Combox Box Group 1 - YES, NA, NO
--> Combox Box Group 1 - YES, NA, NO
--> Combox Box Group 2 - NA
--> Combox Box Group 2 - NA
--> Combox Box Group 2 - NA
I hope my visual presentation looks clear
Copy link to clipboard
Copied
Yes, just add another line of code for the second group. Be sure to change the field group name and switch the list values.
Copy link to clipboard
Copied
Thanks Thom,
if(!event.willCommit)
this.getField("Grp3").getArray().forEach(function(a){a.setItems((event.change=="Option1")?["", "YES", "NO"]:["NA"]);});
this.getField("Grp4").getArray().forEach(function(a){a.setItems((event.change=="Option2)?["NA"]:["", "YES", "NO"]);});
The only problem I found with this is that when you choose Option1 it switches values correctly but when you choose Option2 it sets both boxes to values YES, NO and then you have to choose it again to place NA into Option1 assigned boxes
Any suggestions?.
Copy link to clipboard
Copied
You need to put the code in curly brackets, so it's a single unit that's associated with the if-statement, like this:
if (!event.willCommit) {
this.getField("Grp3").getArray().forEach(function(a){a.setItems((event.change=="Option1") ?["", "YES", "NO"]:["NA"]);});
this.getField("Grp4").getArray().forEach(function(a){a.setItems((event.change=="Option2")? ["NA"]:["", "YES", "NO"]);});
}
Also, you were missing a closing quotation mark after Option2 in the second line.
Copy link to clipboard
Copied
Worked like a charm but only if the script looks like that:
event.change() - need to be set to Option1 in both lines. I don't know why but it only works in that configuration.
Also if I would like to add another line to have 3rd option to keep both Groups "unlocked" with answers "YES" and "NO" just in case user won't pick any option. Does each line controls options from main combo box?
Copy link to clipboard
Copied
The curly brackets define the "if" block. Everything inside the block is controlled by the if condition. That's why you need the curly brackets. Please spend some time to learn about coding in JavaScript.
Each of those lines in the if block controls a different group of fields. If you add a third line that affects either of the two field groups, then it will override the previous code, making that code useless. If you want different conditions for setting the list values, then the code will need to be reconfigured for those conditions.
Also, in this discussion about scripting there is no such thing as "locked" or "unlocked". Please use generic terminology to describe what you want to happen.
Copy link to clipboard
Copied
I would like to auto pop multiple fields with a "NA" if one of 4 selections from a dropdown list is selected. For example, Drop Down Box selections include: i) Married (ii) Unmarried (iii) Widow
If "Unmarried is selcted, I want 5 other felds to auto populate with a "NA".
If Unmarried is not slected, no auto pop
Copy link to clipboard
Copied
Did you read this article?
https://acrobatusers.com/tutorials/change_another_field/
Or look over this information?
https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm