Copy link to clipboard
Copied
Hello everyone,
I'm fairly new at Javascripting and PDF forms, and I would love some help,
I have a list of books with checkboxes which spams for almost 9 hundred items, and I want to count the total number of checkboxes (checked and unchecked); as I have deleted some fields I know that the last numeral isn't the total. So I'd like for a way to calculate it automatically
I've managed to put this code together:
var cntr = 0;
for (var i = 0; i <= this.numFields
; i++)
{
var mf = this.getField(this.getNthFieldName(i));
if (mf.type == "checkbox")
{
cntr++;
}
}
event.value = cntr;
However the number thrown is "685", but the last checkbox numeral is 896; and I didn't delete more than 10-15 fields.
Thank you so much in advance!
Have a great day!
You can create a list of all check boxes:
var cntr = 0;
for (var i = 0; i <= this.numFields; i++) {
var mf = this.getField(this.getNthFieldName(i));
if (mf && mf.type == "checkbox") {
cntr++;
console.println(cntr + " name: '" + this.getNthFieldName(i) + "' pages: " + mf.page);
}
}
Copy link to clipboard
Copied
Are there checkboxes with the same name?
Copy link to clipboard
Copied
they are named as "Book Item #"
Copy link to clipboard
Copied
You can check if there are multiple fields with the same name by adding this to your loop:
if (typeof mf.page=="object") cntr+=mf.page.length;
If the result is not the same as before it means there are fields with more than one instance.
Copy link to clipboard
Copied
I tried inserting it before //if (mf.type == "checkbox")// but now it doesn't show anything, put back the code as before, and still show anything, leaves the field blank
what have I done?
HELP PLEASE!
Copy link to clipboard
Copied
It needs to go after it. Post you full code, and also check the JS Console for errors.
Copy link to clipboard
Copied
Use this code:
var cntr = 0;
for (var i = 0; i <= this.numFields; i++) {
var mf = this.getField(this.getNthFieldName(i));
if (mf.type == "checkbox") {
if (typeof mf.page=="object") cntr+=mf.page.length;
else cntr++;
}
}
event.value = cntr;
Copy link to clipboard
Copied
Alright, it throws this error:
7:Field:Calculate
TypeError: mf is null
I also manually renamed every checkbox as "BookItem###" (no spaces); so they are numbered as 001, 002, ...056, and the last numeral is indeed 685. There were duplicates underneath some checkboxes.
So I'm guessing that the concatenated ### is what is damaging the code.
How can I place "var i" as a 3 digit number?
Copy link to clipboard
Copied
I'm trying this:
var cntr = 1;
for (var i = 1; i <= this.numFields; i++)
{
var num = 0;
if (i<10)
{num.valueAsString = "00" + i;}
if (i<100)
{var num.valueAsString = "0" + i;}
var mf = this.getField(this.getNthFieldName(num));
if (mf.type == "checkbox")
{
if (typeof mf.page=="object")
cntr+=mf.page.length;
else cntr++;
}
}
event.value = cntr;
but it throus me an error after:
if (i<10)
{num.valueAsString = "00" + i;}
if (i<100)
{var num.valueAsString = "0" + i;}
Error: SyntaxError: missing ; before statement
10: line 11
Copy link to clipboard
Copied
Alright, it works like this:
var cntr = 0;
for (var i = 0; i <= this.numFields; i++)
{
var num = "";
if (i<10){num = "00" + i;}
if (i<100){num = "0" + i;}
var mf = this.getField(this.getNthFieldName(num));
if (mf.type == "checkbox")
{
if (typeof mf.page=="object") cntr+=mf.page.length;
else cntr++;
}
}
event.value = cntr;
Although, it counts 30 text fields more... any ideas?
Copy link to clipboard
Copied
Using the variable "nun" makes no sense.
Copy link to clipboard
Copied
True. If you specify the field name there's no need to get getNthFieldName, and vice versa.
Copy link to clipboard
Copied
Hi try67!
I've got it working using the this.getField(this.getNthFieldName(i) code, but I'm learning, so I would like to know how to put the field name "BookItem" instead. I tried this, but throws me an error:
TypeError: f is null
1282:byteCodeTool
This is the code I'm using:
var cntr = 0;
for (var i = 0; i <= this.numFields; i++)
{
var mf = this.getField("BookItem" + i);
if (mf && mf.type == "checkbox")
{
cntr++;
}
}
event.value = cntr;
Thanks so much guys!
Copy link to clipboard
Copied
You can create a list of all check boxes:
var cntr = 0;
for (var i = 0; i <= this.numFields; i++) {
var mf = this.getField(this.getNthFieldName(i));
if (mf && mf.type == "checkbox") {
cntr++;
console.println(cntr + " name: '" + this.getNthFieldName(i) + "' pages: " + mf.page);
}
}
Copy link to clipboard
Copied
Worked!
placed it with event.value like this
var cntr = 0;
for (var i = 0; i <= this.numFields; i++)
{
var mf = this.getField(this.getNthFieldName(i));
if (mf && mf.type == "checkbox")
{
cntr++;
}
}
event.value = cntr;