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

Using checkboxes to input text from text fields

Community Beginner ,
Oct 13, 2022 Oct 13, 2022

Hi, I am struggling to find an elegant solution to a script I would like to run. Simply I have 21 check boxes (var a1 – var a21) that each correspond to a test result. At the end of the test I have a button action that inputs the all results that failed ie. those Checkboxes that aren’t ticked into a textbox. At current I have achieved using several text boxes and the following:

 

// checkboxes for test results

var    a1    =    this.getField("    cb1    ")

var    a2    =    this.getField("    cb2    ")

………

var    a21    =    this.getField("    cb21    ")

 

// text fields for failed results

var    tb1    =    this.getField("    Text1.0    ")

……….

var    tb9    =    this.getField("    Text1.8    ")

 

// text fields that store pre-defined reults

var    bb1    =    this.getField("    bk1.0    ")

…………

var    bb21    =    this.getField("    bk1.20    ")



if(a1.value == "Off" && tb1.value==""){tb1.value = bb1.value;}

if(a2.value == "Off" && tb1.value==""){tb1.value = bb2.value;}

else if(a2.value == "Off" && tb2.value==""){tb2.value = bb2.value;}

 

    if(    a3    .value == "Off" &&    tb1    .value =="") {    tb1    .value =    bb3    .value;}

else    if(    a3    .value == "Off" &&    tb2    .value =="") {    tb2    .value =    bb3    .value;}

else    if(    a3    .value == "Off" &&    tb3    .value =="") {    tb3    .value =    bb3    .value;}

                                   

…….. etc all the way through to var a21.

 

Surely there is a better way to do this in one text box that just lists the failed results from “bb1 –bb21” if the “a1 – a21” aren’t checked? 

 

Many thanks in advance.

 

TOPICS
PDF forms
1.2K
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 ,
Oct 13, 2022 Oct 13, 2022

Yes, there is a much easier way.

1) Get rid of the individual failed result fields (tb)  and replace it with one tall multiline field named "FailedResult"

 

2) Rename the fields so they are easier to script.

    The check boxes can be "cb.0", "cb.1", etc

   Leave the predefined result field names. But "cb.0" needs to correspond to "bk1.0" and so forth

 

3) Use this code

 

 

 

// Get array of checkboxes
var aChecks = this.getField("cb").getArray();

// Initialize combined result text
var strFailedRslt = "";

// Loop over checks and build failed result text
var oDoc = this;
var cFldIdx, oRsltFld;
aChecks.forEach(function(oChk){
          if(oChk.value == "Off"){
              cFldIdx = oChk.name.split(".").pop();
              oRsltFld = oDoc.getField("bk1." + cFldIdx);
              strFailedRslt += oRsltFld.value + "\n";
          }
});

// Set result text
this.getField("FailedResult").value = strFailedRslt;

 

 

 

 

 

This script uses the field grouping feature of Acrobat JavaScript in combination with paralell field naming so we can automatically relate one field to another through the name. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Oct 13, 2022 Oct 13, 2022

Yes, there is a much easier way.

1) Get rid of the individual failed result fields (tb)  and replace it with one tall multiline field named "FailedResult"

 

2) Rename the fields so they are easier to script.

    The check boxes can be "cb.0", "cb.1", etc

   Leave the predefined result field names. But "cb.0" needs to correspond to "bk1.0" and so forth

 

3) Use this code

 

 

 

// Get array of checkboxes
var aChecks = this.getField("cb").getArray();

// Initialize combined result text
var strFailedRslt = "";

// Loop over checks and build failed result text
var oDoc = this;
var cFldIdx, oRsltFld;
aChecks.forEach(function(oChk){
          if(oChk.value == "Off"){
              cFldIdx = oChk.name.split(".").pop();
              oRsltFld = oDoc.getField("bk1." + cFldIdx);
              strFailedRslt += oRsltFld.value + "\n";
          }
});

// Set result text
this.getField("FailedResult").value = strFailedRslt;

 

 

 

 

 

This script uses the field grouping feature of Acrobat JavaScript in combination with paralell field naming so we can automatically relate one field to another through the name. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 Beginner ,
Oct 13, 2022 Oct 13, 2022

Thom,

 

Thanks heaps! I knew there would be a much cleaner way to do it. I can honestly say I have no idea what a lot of those things do but I'm eager to try it out. 

 

Again many thanks for your help.

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 Beginner ,
Oct 23, 2022 Oct 23, 2022

Hi Thom, the above works perfectly. Is there any way to add an incrementing number to the return of each line? Ie.

"(1) Failed result 1

(2) Failed result 2 etc etc."

 

I tried playing around with the 

= oRsltFld.value + "\n"; and could get it to return the right format but couldn't get the number to increment with each result.

 

Many thanks in advance.

 

Luke

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 ,
Oct 24, 2022 Oct 24, 2022

try this modification:

// Get array of checkboxes
var aChecks = this.getField("cb").getArray();

// Initialize combined result text
var strFailedRslt = "";

// Loop over checks and build failed result text
var oDoc = this;
var cFldIdx, oRsltFld, nInc = 1;
aChecks.forEach(function(oChk){
          if(oChk.value == "Off"){
              cFldIdx = oChk.name.split(".").pop();
              oRsltFld = oDoc.getField("bk1." + cFldIdx);
              strFailedRslt += "(" + nInc + ") " + oRsltFld.value + "\n";
              ++nInc;
          }
});

// Set result text
this.getField("FailedResult").value = strFailedRslt;
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 Beginner ,
Oct 24, 2022 Oct 24, 2022

Thanks again Thom, you are a legend. Works perfectly. I tried to also get it to convert to ASCII so I could increment in different formats ie. (A), (B)..... etc but it doesn't seem possible? 

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 ,
Oct 25, 2022 Oct 25, 2022

Yes, letters can be used. But conversion to ASCII requires the correct codes. 

To start at "A", initialize the increment from the char code for "A":

nInc = "A".charCodeAt(0);

 

then to display the letter, convert back to the char code. 

strFailedRslt += "(" + String.fromCharCode(nInc)+ ") " + oRsltFld.value + "\n";

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 Beginner ,
Oct 25, 2022 Oct 25, 2022
LATEST

You. Are. A. Legend! This is perfect, many thanks for your time. I had the first part but couldn't get it back to characters.

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