Highlighted

Need help with code

Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

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;

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

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;

 

TOPICS
Acrobat SDK and JavaScript

Views

74

Likes

Translate

Translate

Report

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

Need help with code

Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

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;

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

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;

 

TOPICS
Acrobat SDK and JavaScript

Views

75

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

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;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

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.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

Again I'm not getting anything, my fields are named "c 1" with space, if I change them to "c.1" then I get this:

Izrezak.PNG

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

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;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 1
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

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


SyntaxError: syntax error
5:Console:Exec
undefined

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

use ";"  not ","  in this line 

 

  if (cV != "Off") cVal += cV,

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

That code worked now. Thank you both for help.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 1
ls_rbls LATEST
Adobe Community Professional ,
Aug 31, 2020

Copy link to clipboard

Copied

What a nice little piece of code...  Thank you!

 

Very useful.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 31, 2020 0