Skip to main content
PMB33163
Participant
September 12, 2023
Answered

Calculating the number of times an item is selected from dropdown lists in a PDF form

  • September 12, 2023
  • 1 reply
  • 1536 views

I am creating a PDF form that has 25 drop-down lists consisting of the following identical options, including:

  • ‘Blank’ (just a single space so the field looks blank)
  • L - Lead
  • GRR - Galvanized Requiring Replacement
  • NL - Non-Lead
  • UNK - Unknown

On another part of the form, I have the same items listed with a text field beside it in a recap section at the end of the form:

Lead ______        GRR ________     NL ________      UNK ________

My goal is that each time a selection is made from one of the lists, the total number of times that selection is made (from all the lists combined) will be displayed in the corresponding recap section.

For example:

If ‘GRR’ is selected total of 8 different times in 8 of the 25 dropdown lists, it will read "8" in the recap section next to the GRR heading.

I am brand new to this – no JavaScript experience at all – so please spell it out for me, and if it would be possible for me to copy and paste the script, that would be AWESOME!

This topic has been closed for replies.
Correct answer Nesa Nurani

Thank you!

The dropdown list names all have two-digits: Material-Category-01 thru Material-Category-25


Then use this:

var L = 0, GRR = 0, NL = 0, UNK = 0;

for(var i=1; i<=25; i++){
 var fieldName = "Material-Category-" + (i < 10 ? "0" + i : i);
 if(this.getField(fieldName).valueAsString == "L - Lead")L++;
 if(this.getField(fieldName).valueAsString == "GRR - Galvanized Requiring Replacement")GRR++;
 if(this.getField(fieldName).valueAsString == "NL - Non-Lead")NL++;
 if(this.getField(fieldName).valueAsString == "UNK - Unknown")UNK++;}

this.getField("L-total").value = L;
this.getField("GRR-total").value = GRR;
this.getField("NL-total").value = NL;
this.getField("UNK-total").value = UNK;

1 reply

Nesa Nurani
Community Expert
Community Expert
September 12, 2023

What are the name of the fields?

Are the values just 'L', 'GRR'...etc  or 'L - Lead', 'GRR - Galvanized Requiring Replacement'...etc?

PMB33163
PMB33163Author
Participant
September 12, 2023

The receiving fields are L-total, GRR-total, NL-total, and UNK-total

Thanks!

Nesa Nurani
Community Expert
Community Expert
September 12, 2023

Ok, assuming dropdown fields are named "Dropdown1-25" use this in one of the fields as custom calculation script:

var L = 0, GRR = 0, NL = 0, UNK = 0;

for(var i=1; i<=25; i++){
 if(this.getField("Dropdown"+i).valueAsString == "L - Lead")L++;
 if(this.getField("Dropdown"+i).valueAsString == "GRR - Galvanized Requiring Replacement")GRR++;
 if(this.getField("Dropdown"+i).valueAsString == "NL - Non-Lead")NL++;
 if(this.getField("Dropdown"+i).valueAsString == "UNK - Unknown")UNK++;}

this.getField("L-total").value = L;
this.getField("GRR-total").value = GRR;
this.getField("NL-total").value = NL;
this.getField("UNK-total").value = UNK;