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

Need help with code

Explorer ,
Aug 30, 2020 Aug 30, 2020

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
1.1K
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

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;

 

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

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

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

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

 

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

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

 

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 ,
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;

 

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

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


SyntaxError: syntax error
5:Console:Exec
undefined

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

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

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

use ";"  not ","  in this line 

 

  if (cV != "Off") cVal += cV,
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 ,
Aug 30, 2020 Aug 30, 2020

That code worked now. Thank you both for 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 Expert ,
Aug 31, 2020 Aug 31, 2020
LATEST

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

 

Very useful.

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