Using Javascript with Acrobat DC forms
Copy link to clipboard
Copied
I have attached a couple of pics. New to Javascript and I am trying to figure out how to either hide multiple Text Fields based on a Checkbox selection or once a Checkbox has been selected I can add text to a Text Field. I know what I am came up with is probably not the ideal way to so something to say the least. Right now I have overlapping fields that I have as either hidden or visible bassed on a Checkbox. Hope this makes sense. Thanks for the help.
if (event.target.isBoxChecked(0)){
this.getField("Paper.5140G").display = display.visible
}
else{
this.getField("Paper.5140G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.5120G").display = display.visible
}
else{
this.getField("Paper.5120G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.BannerS6G").display = display.visible
}
else{
this.getField("Paper.BannerS6G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.CarbonS6G").display = display.visible
}
else{
this.getField("Paper.CarbonS6G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.5030G").display = display.visible
}
else{
this.getField("Paper.5030G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.VacS9G").display = display.visible
}
else{
this.getField("Paper.VacS9G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.5020G").display = display.visible
}
else{
this.getField("Paper.5020G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.VacKitS9G").display = display.visible
}
else{
this.getField("Paper.VacKitS9G").display = display.hidden
}
if (event.target.isBoxChecked(0)){
this.getField("Paper.BannerS9G").display = display.visible
}
else{
this.getField("Paper.BannerS9G").display = display.hidden
}
Copy link to clipboard
Copied
Your code is not very efficient, but it seems correct. Is it not working? If so, are there any error messages when you use it?
To improve it (and make it more legible) I would combine all the commands that fall under one condition into a single block, like this:
if (event.target.isBoxChecked(0)){
this.getField("Paper.5140G").display = display.visible;
this.getField("Paper.5120G").display = display.visible;
// etc.
} else{
this.getField("Paper.5140G").display = display.hidden;
this.getField("Paper.5120G").display = display.hidden;
// etc.
}
Copy link to clipboard
Copied
Thanks for the quick reply. It is working. I just figured there would be a more effcient way to achieve the results.Still learning
Copy link to clipboard
Copied
You can do it like this:
var fields = ["Paper.5140G","Paper.5120G","Paper.BannerS6G","Paper.CarbonS6G","Paper.5030G","Paper.VacS9G","Paper.5020G","Paper.VacKitS9G","Paper.BannerS9G"];
for(var i in fields){
this.getField(fields[i]).display = event.target.value != "Off" ? display.visible : display.hidden;}
Copy link to clipboard
Copied
Hi Nesa, this is working great and thanks for your help. One last question if you don't mind? What would the Javascript look like if I want the opposite? Can that work? The var fields would be "On" but I could make them hidden but selecting a checkbox. Thanks again.
Al
Copy link to clipboard
Copied
If I understand you right you want to hide all those fields with one checkbox?
As Mouse UP event of that checkbox use this:
var fields = ["Paper.5140G","Paper.5120G","Paper.BannerS6G","Paper.CarbonS6G","Paper.5030G","Paper.VacS9G","Paper.5020G","Paper.VacKitS9G","Paper.BannerS9G"];
for(var i in fields){
this.getField(fields[i]).display = display.hidden;}
Copy link to clipboard
Copied
If I then deselect the checkbox I would like fields to become visible again.
Copy link to clipboard
Copied
Regarding populating a text field with a value, that can be done like this:
this.getField("Text1").value = "Some text";

