Skip to main content
Known Participant
August 30, 2020
Answered

Need help with code

  • August 30, 2020
  • 2 replies
  • 1038 views

Hi, I want to export checkboxes values to textfield if it's checked, I have this code but it only works for last checkbox what I'm missing? checkboxes are "c 1" to "c 5".

var cVal = "";
for (var i=1; i<=5; i++) {
cVal =(this.getField("c " + i).value);}
event.value = cVal;

This topic has been closed for replies.
Correct answer Bernd Alheit

Try this:

 

var cVal = 0;
for (var i=1; i<=5; i++) {
  cV =this.getField("c " + i).value;
  if (cV != "Off") cVal += cV;  // <-- corrected
}
event.value = cVal;

 

2 replies

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
August 30, 2020

Try this:

 

var cVal = 0;
for (var i=1; i<=5; i++) {
  cV =this.getField("c " + i).value;
  if (cV != "Off") cVal += cV;  // <-- corrected
}
event.value = cVal;

 

MarietaaAuthor
Known Participant
August 30, 2020

Hi Bernd, thx for helping but I'm getting this error.


SyntaxError: syntax error
5:Console:Exec
undefined

Bernd Alheit
Community Expert
Community Expert
August 30, 2020

The error was a "," it must a ";".

ls_rbls
Community Expert
Community Expert
August 30, 2020

You can try it like this:

 

var f = this.getField("c");
var cVal = f.getArray();
var v = 0;
for (var i=0; i< cVal.length; i++) 
v +=cVal[i].value+" ";
event.value = v;

MarietaaAuthor
Known Participant
August 30, 2020

thanks for your help but that code doesn't do anything. I put it in my text field custom calculation script right?

ls_rbls
Community Expert
Community Expert
August 30, 2020

I made a mistake. And yes run it as custom calculation script.

 

Here it is:

 

var f = getField("c");
var cVal = f.getArray();
var v = "";
for (i =0 ; i < cVal.length ; i++)  v += (cVal[ i ].value+" ")
event.value = v;

 

 

I'm not sure if the way you have the checkbox fields like "c 1", "c 2", "c 3" with a space in between the letter c and then followed by a number will work (but I may be wrong).

 

The getArray() method in Acrobat will assume that a parent name for a field has a hierarchy of  "children" fields.

 

Usually the children field names are prefixed with the parent name and suffixed after a period (or maybe another special character). For example:  myFieldName.1, myFieldName.2, myFieldName.3

 

If you are going to use the array the way you have it , then you may need to consider using  the "new Array()" method.