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

Add JavaScript to Multiple Fields

Explorer ,
Jun 05, 2021 Jun 05, 2021

I have a table of 14x15 rows and columns in Adobe PDF form, and I have to add JavaScript to all the fields/cells. I need to add code in each field separately. Is there a way to add the code all at once?

TOPICS
How to , JavaScript , PDF forms
3.0K
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
1 ACCEPTED SOLUTION
Community Expert ,
Jun 05, 2021 Jun 05, 2021

Yes, you can use script to loop through fields and use setAction to put code into fields.

View solution in original post

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 ,
Jun 05, 2021 Jun 05, 2021

Yes, you can use script to loop through fields and use setAction to put code into fields.

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 ,
Jun 05, 2021 Jun 05, 2021

Thank you for the guidance. It works.

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 ,
Jun 05, 2021 Jun 05, 2021

The same script at any field?

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 ,
Jun 05, 2021 Jun 05, 2021

No, I have different codes for each row and column, but each column has a same incremental code. What I did is, I prepared codes for each column separately, and pasted in a button action as "Mouse up". Here is an example for one column.

// Code for adding Different JavaScript to Multiple Rows 
for ( var i = 2; i < 16; i++)
{
var code = "";
code += "var len = event.target.value.toString().length;";
code += "\n" + "if (len<8) {";
code += "\n" + "    event.target.textSize = 10;";
code += "\n" + "}";
code += "\n" + "else if (len>7) {";
code += "\n" + "    event.target.textSize = 9;";
code += "\n" + "}";
code += "\n" + "if (len>10) {";
code += "\n" + "    event.target.textSize = 8;";
code += "\n" + "}";
code += "\n" + "if (len>12) {";
code += "\n" + "    event.target.textSize = 7;";
code += "\n" + "}";
code += "\n" + "if (len>14) {";
code += "\n" + "    event.target.textSize = 6;";
code += "\n" + "}";

code += "\n" + "//////////////////////////////////////////////";
code += "\n" + "var c = this.getField(\"Pier IDRow" +i+ "\").valueAsString;";
code += "\n" + "var v = this.getField(\"Soil Type at Pier BaseRow1\").valueAsString;";
code += "\n" + "if (c==\"\") event.value = \"\";";
code += "\n" + "else event.value = v;";
code += "\n" + "";

var rname = "Soil Type at Pier BaseRow"+i;
this.getField(rname).setAction("Calculate", code);
this.calculateNow();
}
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 ,
Jun 05, 2021 Jun 05, 2021
LATEST

When you use document level functions it is easier to change and debug the code.

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