PDF Form Java Script to transfer values of Check Box to a Text Field in a proper

Previous topic - Next topic

durmul

Inspired by a video on PDF Form javascript to transfer values of Checkbox to a text field in a proper listed format
I tried to name the checkbox field in a sequence manner cHob.01, but dot(.) is not accepted in name field of checkbox, I then named my name field sequence in cHob1, cHob2, cHob3 and then I created the declared variables and set an array as described in video here is the code, but its not working
var items = [ ];

for (i = 0; i<10; i++) {

var Hobbies = this.getField("cHob1"+ i);

if (Hob.value !="Off"){

items.push(items.length+1 + ". " + Hob.value);
event.value = "Your Hobbies are listed below: "+  "\n" + items.join("\n");
}
if(items.length<=0) {event.value = " "}
}

The above code works fine in acrobat dc pro as in video but not in scribus.


[attachment deleted by admin]

jghali

While I'm not a JavaScript specialist, I think this line is wrong:
var Hobbies = this.getField("cHob1"+ i);

This would likely give you cHob10, cHob11, cHob12 as field names instead of the wanted cHob1, cHob2, cHob3.

durmul

I tried with renaming fields to cHob10 and so on also change the Item fields names to Hob10... and so on

It's not working

There was error earlier in the code rectified instead of Hobbies it should have been Hob
var items = [ ];

for (i = 0; i<19; i++) {

var Hob = this.getField("cHob10"+ i);

if (Hob.value !="Off"){

items.push(items.length+1 + ". " + Hob.value);
event.value = "Your Hobbies are listed below: "+  "\n" + items.join("\n");
}
if(items.length<=0) {event.value = " "}
}

jghali

Again this line is wrong:
var Hob = this.getField("cHob10"+ i);

This code will not increment field names to cHob10, cHob11, cHob12, etc...

durmul

Guide me what to write

I am also not javascript expert Just beginner

I saw the video copied it and tried to use it in scribus

Nermander

"cHob10" is a string and i is a number.

While I do not know much about javascript, I know that you cannot add numbers to a string.

Usually you would have a string like "cHob" and then to that string append a string with the number. So the number i first needs to be converted to a string, and then the two strings concatenated.

Though it may be that javascript converts a number to string automatically. But it will likely not prepend leading zeroes.

So using "cHob0"+ i will give you a sequence like:
cHob00
cHob01
cHob02
...
cHob09
cHob010
cHob011
cHob012
etc.

durmul

Thanks
Now i am getting

Your Hobbies are listed below: 1. Yes2. Yes 3. Yes

and so on.... see the screenshot

[attachment deleted by admin]