Skip to main content
Participating Frequently
February 18, 2020
Answered

Fields hidden until all button groups have been selected

  • February 18, 2020
  • 1 reply
  • 1733 views

Greetings all!

I have a form which contains a series of radio button groups which, when selected, will generate a total score in a textbox based on which buttons are selected. I am looking for the specific functionality of having the total textbox hidden until a button from each group has been selected, rather than the total box showing a running tally as a selection is made in each group.

 

Basically, I have a textbox: "Total" which will be calculated by the points earned in Groups 1, 2, 3, & 4. Currently, when no buttons are selected, the Total field displays "0". When a selection of "5" is made in Group 1, the Total field now displays "5". What I want is for the Total field to remain hidden until a button has been checked in Groups 1, 2, 3, & 4.

 

I believe the solution to my question involves writing JavaScript in either the Calculate or Validate tab in the "Total" textbox. I have no experience with JavaScript and all forums I've found up to this point don't quite get at my exact issue. Any help

This topic has been closed for replies.
Correct answer Thom Parker

Thanks again! That worked perfectly!

 

Now that my form is working properly, I've identified one final function I'd like the form to have. As this form is yielding a grade of a performance, I'd love for there to be a way for the text "NO GO" to appear in the "Grand Total" field should either of the following criteria be met:

1) The total final score is below 80%

2) If one of more radio buttons in the "Not Proficient" or "0" column are selected.

To explain #2 a bit, my form has 5 columns worth 5,4,3,2,0 respectively (which I've done by assigning the corresponding number to the Radio Button Choice in each of the buttons in each group). If a student earns any "0"s, the entire performance must be redone and reassessed.

 

So if a student earns an 85%, I'd like "85%" to display in the Grand Total field (This is currently happening).

If a student earns a 75%, I'd like the Grand Total field to display "75% NO GO"

If a student scores ANY "0"s (regardless of whether or not the final % is over 80), I'd like the Grand Total field to display "XX% NO GO"

 

Thanks in advance!


This type of display behavior is best handled with a custom format script. Formatting changes the display of a field, but not the underlying value. 

 

You can read more about it here:

https://acrobatusers.com/tutorials/formatting_text_fields/

 

1 reply

Thom Parker
Community Expert
Community Expert
February 18, 2020

Read this article:

https://acrobatusers.com/tutorials/show_hide_fields/

 

You can find out more about radiobuttons and checkboxes here:

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

Now, for your specific solution you'll need to use a calculation script in the "Total" field. It is best to group the radio button groups so they can be handled together.

By this I mean name grouping. So "Area1.RadioGroup1" "Area1.RadioGroup2", etc. So Area1 is the field name group.

 

So this code gets a list of all the radio button groups

 

var aRadGrps = this.getField("Area1").getArray(); 

// This tests to see if all fields are selected

if(aRadGrps.every(function(a){return a.value != "Off"}))

{ // Now you can calculate the total and make it visible

    event.target.display = display.visible;

    var sum = 0;

    aRadGrps.forEach(function(a){sum+=a.value});

    event.value = sum;

}

else // Make hidden

    event.target.display = display.hidden;

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
February 19, 2020

Greetings Thom,

 

Thank you for your reply! The code worked great for my radio button groups! Now I'm trying to get the same functionality from my "Sub-total" fields to my "Total" fields to my "Grand Total" field.

 

My radio button groups are "Area1.RadioGroupX"-"Area10.RadioGroupX".

 

I named 5 of my 10 Sub-total Fields "Area11.TextBox1" "Area11.TextBox2"..."Area11.TextBox5" and the other 5 are "Area12.TextBox1"..."Area12.TextBox5".

 

I need the TextBoxes in Area11 to add up and display in "Area13.TextBox1", and I need Area12 to add up and display in "Area14.TextBox1". Then I need Area13 and Area14 to average together and display in "Area15.TextBox1" for a grand total.

 

I tried to take the code you provided to total up my radio boxes and rewrite it to refer to the desired textboxes instead, it's halfway working... Here's what I created based off of your code:

 

var aTextBox = this.getField("Area11").getArray();

if(aTextBox.every(function(a){return a.value != "Off"}))

{event.target.display = display.visible;

var sum = 0;

aTextBox.forEach(function(a){sum+=a.value});

event.value = sum;}

else // Make hidden

event.target.display = display.hidden;

 

Like I said, it's only halfway working. It'll correctly add up the fields if I fill them out in order, but if I start from the bottom and work my way up, the total values stack in front of eachother (instead of 20+40=60, I get 2040, etc.). Additionally, the field isn't hidden until all subvalues have been created.

 

Thanks again for your help, it is greatly appreciated!!!

Thom Parker
Community Expert
Community Expert
February 19, 2020

The test in the "if" statement is specific to radiobuttons, i.e., it tests for a value of Not "Off". For text fields that contain numbers you'll need to test for an empty field or 0. 

 

if(aTextBox.every(function(a){return (a.value != "") && (a.value != 0)}))

 

For the summation, the "+" operator in JavaScript is both addition and concatonation. Which operation it performs depends on whether it value is interpreted as a number or string. And So, the code needs to be explicit.

 

aTextBox.forEach(function(a){sum+=Number(a.value)});

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often