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

Need help with code

Explorer ,
Aug 30, 2020 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;

TOPICS
Acrobat SDK and JavaScript

Views

436

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
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 30, 2020 Aug 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;

 

Votes

Translate

Translate
Community Expert ,
Aug 30, 2020 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;

Votes

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
community guidelines
Explorer ,
Aug 30, 2020 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?

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 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.

 

Votes

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
community guidelines
Explorer ,
Aug 30, 2020 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

 

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 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;

 

Votes

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
community guidelines
Explorer ,
Aug 30, 2020 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

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

use ";"  not ","  in this line 

 

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

Votes

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
community guidelines
Explorer ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

That code worked now. Thank you both for help.

Votes

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
community guidelines
Community Expert ,
Aug 31, 2020 Aug 31, 2020

Copy link to clipboard

Copied

LATEST

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

 

Very useful.

Votes

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
community guidelines