Skip to main content
Participant
November 7, 2016
Question

Javascript calculations and Radio button values

  • November 7, 2016
  • 2 replies
  • 2901 views

I have a couple questions regarding how to create calculations on a 150 question quiz using radio buttons that I have created.

Each question has 3 choices. "yes" "No" or "?".  Each question is labeled as its coresponding number  (1,2,3,4,5, etc.) and the radio button choices are currently labeled as "Yes", "No, or "?".

I need to assign a "1" to the correct answer and a "0" to the incorrect ones for each question (sidenote - a few questions have 2 acceptable answers, so two choice's would need to be labeled "1" and in all other instances there will be assigned values of "1", "0", and "0" <---does doing this then allow the user to click more than one button? I only want them to be able to choose a single answer for each qeustion.)

Do I need to go back and relabel all 150 x 3 radio button choices to numbers to act as export values?

Finally, Ill need to create a caculation based on the choices selected. Can I do this using the simple calculation function in a text field if I assign the values like I previoiusly mentioned? OR should I be using some sort of simplified or custom javascript in this instance? Apologies for the longest question ever. I am so stuck and would greatly appreciate any help at all.

This topic has been closed for replies.

2 replies

JR Boulay
Community Expert
Community Expert
November 8, 2016

Hi.

Assuming that your field are named (the dot really matters):

radio.1

radio.2

radio.3

etc.

You just have to use the built-in Acrobat's calculation and pick the parent field only. As shown on the screengrab.

See this demo PDF file: Shared Files - Acrobat.com

Acrobate du PDF, InDesigner et Photoshopographe
Participant
November 8, 2016

Thank you. I am just currently using that feature and it seems to be working, YAY! However, I did NOT change my feild names, they are still simply just "1", "2", "3"....will I run into problems if I keep them this way?

JR Boulay
Community Expert
Community Expert
November 8, 2016

You must use at least one dot in field names to create a "virtual" parent field.

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
November 7, 2016

It would probably be best if you changed the export values of each radio button to a number. It's OK for what you're doing if more than one in the group share the same export value. It becomes a problem if you want to programmatically set the field values or import form data.

If you do this and you have field names like "radio1", "radio2", ..."radio150", you can use a relatively simple JavaScript to perform the calculation. The other options will be a lot more cumbersome with that many fields involved. The script could be something like:

// Custom calculation script

(function () {

    // Initialize variables

    var i, val, sum = 0;

    // Loop through the radio buttons and add numeric value of selected button to sum

    for (var i = 1; i <= 150; i += 1) {

        val = getField("radio" + i).value;

        if (val !== "Off") {

            sum += +val;

        }

    }

    // Set this field's value to the sum

    event.value = sum;

})();

You'd replace "radio" in the script with whatever your field name prefix is.

Participant
November 8, 2016

Good Morning George_Johnson,

Thank you very much for the information! I will change the radio button choices to numerical values!

I currently have the radio button field names set to numbers; 1,2,3,4...150. Do I need to name them something else for this to work? You mentioned a "field name prefix"

Also, I should have mentioned in my first post that this 150 question test is going to be scored in 5 categories. So I will need to create 5 text fields, each coresponding to 30 of the 150 questions, to score this quiz based on the categories.

The questions which pertain to each category are placed in a numerical pattern but not consecutive (so as not to have the test taker realize the scheme). For example - 1,6,11,16,21,26,31,36 in category "A" and for category "B" - 2,7,12,17,22,27,32,37 category "C" - 3,8,13,18,23,28. I wasn't sure if this will complicate/change the JavaScript text needed. It looks like the example you sent me would only work for scoring this 1 through 150 (which is understandable based on the information, or lack there of, that I gave in my original post, sorry .)

Finally, Will I be entering the javascript into the Custom calculation field or the simplified field?

Thank you again this forum is a life saver for a beginner like myself.

Brooke