Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Radio button JavaScript help please!

Community Beginner ,
Aug 29, 2018 Aug 29, 2018

so I have 3 groups of 5 radio buttons each. Each radio button has a value 1-5 For each group.

What i am Trying to do is if I click average the button values and have the result show up on a 4th group of radio buttons if the average falls between certain numbers.

there Are also 5 results button.

I need the result button number 1 to be click if any of the groups clicked the first button with the value 1.

I need the result button number 2 to be checked if any of the number 2 button in any of the groups 1-3 is clicked. So no matter what the other groups have if a 1 is clicked then the result is 1, if the 2 is clicked, then a 2 is the result.

So only if there are no 1s or 2 checked in the first 3 groups, I need it to average the Radio buttons. So if the three were all 5s then the average is 5 and the group 4 button 5 would be clicked. If one 4 and 2 5s average to 4.6 I need the 5th button to be selected On group 4

so if the top 3 groups average is >=4.6 and <=5 then the 5th button is checked.

If the average is between 3.6 and <4.6 the result would be button 4.

3 to <3.6 would be a 3.

any help would be greatly appreciated

thank you!!

TOPICS
PDF forms
1.3K
Translate
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
1 ACCEPTED SOLUTION
LEGEND ,
Aug 31, 2018 Aug 31, 2018
LATEST
Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

Since all radio buttons, unless otherwise setup, start in the "Off" value, how are they to be averaged?

How are buttons not selected to be handled by by the averaging function?

This code will all be custom JavaScript, so each field name needs to be known?

Your rounding is not the usual bankers rounding or rounding up at .5, so this will require some custom coding for the rounding.

Translate
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
Community Beginner ,
Aug 29, 2018 Aug 29, 2018

Thank you for the quick reply.

The buttons are named group2button1, group2button2, group2button3, all the way to 5.  And the same for groupsa 3, 4, and 5.

Button one has a value of 1. Button 2 has a value of 2. So on and so forth.

Each group must be answered so the average average would be the addition of those 3 buttons divided by 3.

That answer then would result in a button in group 5 being clicked depending on where it falls in the ranges provided above.

im not sure there needs to be any rounding as long as the number fits between those ranges.

Thank yoh for any help you can give. I’m obviously JavaScript dumb.

Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

Have you checked you button actions to see if they are mutually exclusive?

Usually radio buttons within a group share a common "Group Name" like "Group2" and then each individual button within the group has "Radio Button Choice". which is the value of that specific button when selected. I would set the "Radio Button Choice" to the number value I wanted for that selected button. This makes have the buttons behave in a mutually exclusive manner and the value of the selected buttons used in the calculations so much easier.

I see the tenths for the average being "0", "0.3333333333",  or "0.666666666". This will require rounding since the value of a radio button group has to be a whole number.

Translate
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
Community Beginner ,
Aug 29, 2018 Aug 29, 2018

They are mutually exclusive.  They are named group2. Group3. Group4. Group5.

Each has their own set of 5 radio buttons

Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

Your code might be:

// custom claculation script for a text field to compute the average of a set of fields;

event.value = ""; // clear the resutl;

var aFieldNames = new Array("Group2", "Group3", "Group4"); // field names to cmopute average for;

// some variables for cmputed values;

var nCount = 0; // count of non-Off selections;

var nSum = 0; // sum of non-Off selections;

var nAverage = ""; // computed average;

var iAverage  // radio button value for average

var oElement; // value of group being processed;

for(var i = 0; i < aFieldNames.length; i++) {

// loop through the fields;

oElement = this.getField(aFieldNames);

if(oElement == null) {

// trap error accessing any field;

app.alert("Field named " + aFieldNames + " not found!", 1, 0);

} else {

// process a given field;

if(oElement.value != "Off") {

nCount++; // increment the count;

nSum += Number(oElement.value); // add value to the sum;

}

}

}

if(nCount > 0) {

// compute the average;

nAverage = nSum / nCount;

// round the average to a whole number at 0.6 & up;

switch(true) {

case (nAverage <= 1.5):

iAverage = 1;

break;

case (nAverage <= 2.5):

iAverage = 2;

break;

case (nAverage <= 3.5):

iAverage = 3;

break;

case (nAverage <= 4.5):

iAverage = 4;

break;

case (nAverage <= 5):

iAverage = 5;

break;

default:

iAvarage = "Off";

break;

}

}

event.value = nAverage; // computed average;

this.getField("Group5").value = iAverage; // set radio button average;

// some optional code for checking the calculation;

console.clear();

console.println("Sum = " + nSum + " Item count = " + nCount + " Average = " + nAverage);

Note you will have to add text field for the calculation since radio buttons do not have a calculation option and "Group5" is the radio button group for displaying the result.

https://www.dropbox.com/s/6ib2mup8l166jat/AverageRadioButton.pdf?dl=0

AverageRadioButton

Translate
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
Community Beginner ,
Aug 30, 2018 Aug 30, 2018

I guess I’m dumb.  I downloaded your pdf and inserted the code above and it didn’t seem to work. I appreciate the help tho.  Thank you!

Translate
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
LEGEND ,
Aug 30, 2018 Aug 30, 2018

JavaScript is a programming language and like most computer programming languages everything needs to be correct. You need to spell and capitalize field names correctly, and configure the fields correctly. You may want to look closely at the form and the various setting for each field.

Translate
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
Community Beginner ,
Aug 30, 2018 Aug 30, 2018

Would you be able to add the code to your drop box spreadsheet and see if it works for you? I don’t see any typing errors for group names.  Thank you!

Translate
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
LEGEND ,
Aug 31, 2018 Aug 31, 2018
LATEST

Here is the link form above:

AverageRadioButton

Translate
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