Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using Javascript with Acrobat DC forms

Explorer ,
Sep 20, 2021 Sep 20, 2021

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.1Capture.PNGexpand image

Capture.PNGexpand image


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
}

TOPICS
JavaScript , PDF forms
929
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2021 Sep 20, 2021

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.
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 20, 2021 Sep 20, 2021

Thanks for the quick reply. It is working. I just figured there would be a more effcient way to achieve the results.Still learning

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2021 Sep 20, 2021

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;}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2022 Jan 05, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2022 Jan 05, 2022

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;}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2022 Jan 05, 2022
LATEST

If I then deselect the checkbox I would like fields to become visible again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2021 Sep 20, 2021

Regarding populating a text field with a value, that can be done like this:

 

this.getField("Text1").value = "Some text";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines