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

Count Total number of Checkboxes

Community Beginner ,
Jul 16, 2019 Jul 16, 2019

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!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.6K

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 , Jul 18, 2019 Jul 18, 2019

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

    }

}

Votes

Translate

Translate
Community Expert ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

Are there checkboxes with the same name?

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 Beginner ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

they are named as "Book Item #"

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 ,
Jul 17, 2019 Jul 17, 2019

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.

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 Beginner ,
Jul 17, 2019 Jul 17, 2019

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!

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 ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

It needs to go after it. Post you full code, and also check the JS Console for errors.

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 ,
Jul 17, 2019 Jul 17, 2019

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;

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 Beginner ,
Jul 17, 2019 Jul 17, 2019

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?

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 Beginner ,
Jul 17, 2019 Jul 17, 2019

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

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 Beginner ,
Jul 17, 2019 Jul 17, 2019

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?

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 ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

Using the variable "nun" makes no sense.

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 ,
Jul 18, 2019 Jul 18, 2019

Copy link to clipboard

Copied

True. If you specify the field name there's no need to get getNthFieldName, and vice versa.

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 Beginner ,
Jul 24, 2019 Jul 24, 2019

Copy link to clipboard

Copied

LATEST

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!

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 ,
Jul 18, 2019 Jul 18, 2019

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

    }

}

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 Beginner ,
Jul 24, 2019 Jul 24, 2019

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;

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