Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Radio button averaging

New Here ,
Jan 15, 2021 Jan 15, 2021

Copy link to clipboard

Copied

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!

TOPICS
PDF forms

Views

192

Likes

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

Adobe Community Professional , Jan 16, 2021 Jan 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")....

Likes

Translate

Translate
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

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.

Likes

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
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

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.

Likes

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
New Here ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

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?

Likes

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
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

Yes, but signing and submitting is not the same thing. The required fields are only validated when you submit the form, not when you sign it, save it, print it, or anything like that.

Likes

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
New Here ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

I see.

The way this particular form is going to work is that i send it out with the studen't name already filled out and the instructor fills out the 8 groups of radio buttons on a 1-5 basis of 8 criteria (along with other text fields that have no bearing on this issue) and then they sign it and click the email button which i already have the css coding in there to send it back to my inbox.  So that being said, when they go to send it, it would check to check to make sure all fields are filled out?

Also, if I were to not make the buttons required, how can i add the failsafe you spoke of into Nesa's code?

Likes

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
Enthusiast ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

Don't worry, if you are gonna select choice in each of 8 group every time, Nesa'a code will work.

What try67 is saying it would give an error if one of the groups is not selected, but since you writed that you are selecting all 8 groups there is nothing to worry about.

Likes

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
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

I was under impression that you will always select one choice in all 8 groups.

But if you want you can try this code:

var total = 0;
var total2 = "";
for (var i=1; i<=8; i++) {{
if(this.getField("Group"+i).valueAsString!= "Off")
total+=Number(this.getField("Group"+i).valueAsString)
this.getField("Group"+i).readonly = true;}
if(this.getField("Group"+i).valueAsString!= "Off")total2++}
this.getField("Text1").value = total/total2;

I think this should calculate even if not all fields are selected and it will divide by the number of groups selected.

Likes

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
New Here ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

What i did was change my field names to match your code (it was the easier route).

Now when i sign the document as a test, the text field gives me NaN.

I'm assuming that even though i gave each radio button choice a number (1-5), i may need to give it a number value somehow so the code can do the math? 

Am i correct and if so, how do i do that? Go into each of the properties and do it 40 times?

Likes

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
New Here ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

And you are correct, they will be choosing one in each of the groups every time.

Likes

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
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

If you gave them choice 1-5 that should be their value.

Can you share your file, it would be easier to see where is the issue?

Likes

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
New Here ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

Likes

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
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

It's working fine, you probably didn't select option in all 8 fields.

Thats why you get NaN.

Also since you said you are gonna select each 8 field getting NaN in this case is good so you can know you made a mistake.

 

Likes

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
New Here ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

LATEST

Fantastic! You guys are rockstars and you're making me look like a rockstar. I hope to really get the theory of this code down better!

Thanks Agian!

Likes

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