Skip to main content
Dan6777
Participating Frequently
January 16, 2021
Answered

Radio button averaging

  • January 16, 2021
  • 1 reply
  • 3118 views

Hey Folks!

I'm a teacher that has recently been thrust into making forms to make the remote part of my job easier. Here is my dilemma:

I have 8 sets of radio buttons that have a value of 1-5, I need to add up each set and average them and display the results to a read-only text field After the document is digitally signed. (its a student evaluation form). 

Each line result doesnt need to be visible, just the result. 

I have a passing knowledge of javascript so i know i would have to have the signature box perform a script when signed so i guess this would have to be put in there? Plus, if i wanted to make every box readOnly when it is signed, is it easier to do it in the signature commands or ina script?

 

Thanks for your help!

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

You can do it in a signature field as a script, something like this:

var g1 = Number(this.getField("Group1").valueAsString);
var g2 = Number(this.getField("Group2").valueAsString);
var g3 = Number(this.getField("Group3").valueAsString);
var g4 = Number(this.getField("Group4").valueAsString);
var g5 = Number(this.getField("Group5").valueAsString);
var g6 = Number(this.getField("Group6").valueAsString);
var g7 = Number(this.getField("Group7").valueAsString);
var g8 = Number(this.getField("Group8").valueAsString);

this.getField("Text1").value = (g1+g2+g3+g4+g5+g6+g7+g8)/8;

this.getField("Group1").readonly = true;
this.getField("Group2").readonly = true;
this.getField("Group3").readonly = true;
this.getField("Group4").readonly = true;
this.getField("Group5").readonly = true;
this.getField("Group6").readonly = true;
this.getField("Group7").readonly = true;
this.getField("Group8").readonly = true;

Change field names to whichever names you have.

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
January 16, 2021

You can do it in a signature field as a script, something like this:

var g1 = Number(this.getField("Group1").valueAsString);
var g2 = Number(this.getField("Group2").valueAsString);
var g3 = Number(this.getField("Group3").valueAsString);
var g4 = Number(this.getField("Group4").valueAsString);
var g5 = Number(this.getField("Group5").valueAsString);
var g6 = Number(this.getField("Group6").valueAsString);
var g7 = Number(this.getField("Group7").valueAsString);
var g8 = Number(this.getField("Group8").valueAsString);

this.getField("Text1").value = (g1+g2+g3+g4+g5+g6+g7+g8)/8;

this.getField("Group1").readonly = true;
this.getField("Group2").readonly = true;
this.getField("Group3").readonly = true;
this.getField("Group4").readonly = true;
this.getField("Group5").readonly = true;
this.getField("Group6").readonly = true;
this.getField("Group7").readonly = true;
this.getField("Group8").readonly = true;

Change field names to whichever names you have.

try67
Community Expert
Community Expert
January 16, 2021

This is a good approach, but with one caveat: If the user didn't make a selection in any of the radio-button groups it will fail, since its value will be "Off", which can't be converted to a number. So you need to adjust the code to first check if the value is not Off, and only then convert it to a number. Also, you need to decide whether you want to include a field that hasn't been filled-in in the average. If so, replace "Off" with 0. If not, then you'll need to keep a counter of the number of answered questions to divide by, instead of hard-coding it as 8.

Dan6777
Dan6777Author
Participating Frequently
January 16, 2021

Since i made all of these choices "required" wouldnt it alert the user when the send button is clicked that there are highlighted fields to be selected before the document could be sent? This, to me eliminates the "off" error. Is that correct?