Skip to main content
Known Participant
July 17, 2019
Answered

Count Total number of Checkboxes

  • July 17, 2019
  • 1 reply
  • 2675 views

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!

This topic has been closed for replies.
Correct answer Bernd Alheit

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?


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

    }

}

1 reply

Bernd Alheit
Community Expert
July 17, 2019

Are there checkboxes with the same name?

Known Participant
July 17, 2019

they are named as "Book Item #"

try67
Community Expert
July 18, 2019

Using the variable "nun" makes no sense.


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