Acrobat: Advanced Radio Buttons

I have an Acrobat numeric form field which contains a calculated value. The value is not "read only".

There are a pair of radio buttons, which toggle (same name; different export values).

When I press Button A, I want to add $3 to the value of an the numeric form field. When I press Button B, I want to add $5 to the value of the numeric field.

When the form loads, the buttons both need to be blank. Then whichever one is pressed will perform as above.

However, if the user changes his mind and selects the alternate button after already making a selection, the script has to subtract out the button being blurred before adding in the value from the button getting the focus.

Is this possible, or is there a better way to solve the problem of adding in one of two supplemental values to the numeric field?

Thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karl Heinz KremerCommented:
You should make the text field read-only. This only applies to changes done interactively. Because the field contains a calculated value, nobody should be able to modify the field with mouse/keyboard.

Do you want the user to be able to deselect both radio buttons/checkboxes after one was selected?
goodmoralsAuthor Commented:
I have found that once I click a paired radio button that I do not know how to return both buttons to an unchecked state. I need to save the form so they are both unchecked when the user loads the form in Reader.

The user has to check one button or the other, so there is no need for the user to be able to deselect both after one has been selected.

The $3 or $5 value added into the numeric field should be subtracted out only when a button is already checked when deselected (and then the amount associated with the newly-selected button is added in its place).

Thank you again for your help.
Karl Heinz KremerCommented:
I've implemented this with two independent radio buttons, and two check-boxes to store a mirror of the radio button settings. This is necessary to be able to determine the previous setting (you don't want to add another $5, just because the user clicked on the radio button again.

The following document level script (select "Advanced>JavaScript>Document JavaScript..." to add this script) assumes that you have one text field named "Text1", two radio buttons named "RadioButton1" and "RadioButton2", and two check boxes named "CheckBox1" and "CheckBox2". You need to make the checkboxes "hidden", so that the are not available to the user.

function selectButton(button_nr)
    var f1 = this.getField("RadioButton1");
    var f2 = this.getField("RadioButton2");
    var f3 = this.getField("Text1");
    var f4 = this.getField("CheckBox1");
    var f5 = this.getField("CheckBox2");
    var this_button, other_button;
    var this_value, other_value;
    var this_hidden, other_hidden;

    if (button_nr == 1)    // the first button (A) was selected
        this_button = f1;
        this_value = 3;
        this_hidden = f4;
        other_button = f2;
        other_value = 5;
        other_hidden = f5;
    else                    // the second button (B) was selected
        this_button = f2;
        this_value = 5;
        this_hidden = f5;
        other_button = f1;
        other_value = 3;
        other_hidden = f4;

    var new_value = f3.value;

    // is the other button selected?
    if (other_button.value == "Yes")
        // subtract the value "other_value" and assign it to the field
        new_value -= other_value;              

        // deselect the other button and the hidden "mirror"
        other_button.value = "Off";
        other_hidden.value = "Off";

    // Is the this button un-selected (otherwise, it was already selected, and
    // we don't have to do anything). We cannot use the current button state, because
    // this is already set to "selected" (we just clicked with the mouse on it). We
    // are therefore using the hidden "mirror" of this button for this purpose.

    if (this_hidden.value != "Yes")
        // add "this_value"
        new_value += this_value;

        f3.value = new_value;

        // ... and select the mirror checkbox
        this_hidden.value = "Yes";
// end of script

Then create a JavaScript "Mouse Up" action for the first button (A) that looks like this:


and, create a JavaScript "Mouse Up" action for the second (B) button tha tlooks like this:


There is however another way to do this: You can create a calculation script that takes the input value, and the state of your (in this case group of) radio buttons and then calculates the output value whenever the state of the radio button changes, or when you do an explicit recalculation of the form. This way, the logic behind the buttons is much simpler, but you need to be able to calculate the input value (that may not be displayed anywhere on your form.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

goodmoralsAuthor Commented:
I am wondering how to identify "RadioButton1" and "RadioButton2" in the code when they both have the same form field name, but different export values. Can you give me some guidance on distinguishing them for code purposes?

Karl Heinz KremerCommented:
The reason I used to different radio buttons is that it's not possible to distinguish the different events otherwise. As I said before, if you are limited to just one radio button group, you need to use a calculation to determine the output value based on the selected radio button and the input value.

Use this as document level JavaScript:

function calculateOutputValue()
    var inputValue = ... // calculate the input value
    // var inputValue = this.getField("inputValue").value;
    var outputField = this.getField("outputValue");
    var radioButton = this.getField("RadioButton1");
    if (radioButton.value == 1)
        outputField.value = inputValue + 3;
        outputField.value = inputValue + 5;
// end of script

You need to provide a method to get/calculate the inputValue. In the commented out line, I'm just getting it from a form field named "inputValue". you can use this to experiment with the function.

This assumes that you have an output field named "outputValue", and a radio button group with two buttons named RadioButton1 that uses the values 1 and 2 as export values.
goodmoralsAuthor Commented:
Thank you for your help with this matter.

I have tried out the solution and find that the radio buttons do not deselect once selected. Also, the code will permit adding $3 or $5, and then if you switch to the other button, it adds in the other amount also, and both buttons are selected. There is no subtraction of the deselected amount, or deselecting of the other button. At that point, both checkboxes are checked, and no further amounts can be added.

When the form first loads, the buttons come up both selected.

Thank you for your help in unravelling this.

The field names I am using are:

var f1 = this.getField("SC.1");
    var f2 = this.getField("SC.1a");
    var f3 = this.getField("Pymt.1");
    var f4 = this.getField("CB.1");
    var f5 = this.getField("CB.1a");

I have loaded the rest of the code into document javascript just as written.
Karl Heinz KremerCommented:
No, it works for me (both of them actually). Which version of Acrobat are you using (please be specific e.g. 6.0.2 Professional)?
Are you sure you used the radio buttons, and not the check boxes to make the selection?
Karl Heinz KremerCommented:
To reset the form (so that both radio buttons are unselected), add the following _BEFORE_ the function definition of "selectButton()" (this has to be outside of the function, so that the code gets executed when you load the document):


This will reset the form to the default values.

I just created a document from scratch, copied the code from this thread (so that any potential problems with typos in the code that I posted are caught), even used the variable names from your last comment, and it works.

Open up the JavaScript console before you load the document, then load the document, click on the radio buttons and watch for any output to the debug dialog. Do you see any error messages?
goodmoralsAuthor Commented:
I uploaded the file to:

Just click on the link and it will pull up the pdf.

It is almost a megabyte so may take a little while to load.

Then you can download it to desktop and analyze.

I put the resetForm code in Recalculate01 under Document Javascript.

There are 10 lines in the form, so all the code is set up for ten uses.

However, the radio button test on the far right is only on line 1.

I have the two checkboxes visible so you can see them work.

You need to select a creditor name from the combo box and then enter a Balance Owed to create the payment field.

ALternatively you can skip this and just test the buttons without it. They will add $3/$5 to the payment field. Let me know if you experience the same problem I described. Both buttons add amounts, but don't alternate.

Thanks again for your help with this.
goodmoralsAuthor Commented:
I have 6.0.1 Professional.
Karl Heinz KremerCommented:
Found it. You changed the export value for the radio buttons from "Yes" to "Current" and "Past Due". The script does however check for the value "Yes", so it is not able to determine if a button is selected. This is why the "other button" does not get deselected when you click on the second radio button.

I also noticed that you could make your document level scripts a lot more straight forward: There is no need to have multiple instances of the recalculate script. Just create one, and pass a parameter into the script:

function recalculate(var ctrl)
    var balOwed = this.getField("BalOwed." + ctrl).value;
    var exportValue = this.getField("Cred." + ctrl).value;

    var newValue = Math.ceil(gPctList[exportValue] * balOwed);

    if (newValue < gMinAmt[exportValue])
        newValue = gMinAmt[exportValue];

    var f = this.getField("NewAPR." + ctrl);
    f.value = gNewAPR[exportValue];;

    f = this.getField("Pymt." + ctrl);
    f.value = newValue;

Now, when you call the script, don't call recalculate01(), but instead use recalculate(1).

You can add the this.resetForm() call to your "Global" document level script.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goodmoralsAuthor Commented:
Just one question and I think we are done.

Is the export value for both buttons "Yes"? Or is there a "Yes" and a "No"?
Karl Heinz KremerCommented:
It's "Yes" for both buttons (it just indicates that the button is selected, I'm not using it for anything else). Keep in mind that the default values you see for a radio button are "Off" and "Yes"... not very intuitive. Every now and then, I expect to see "On" (or "No" for the "Off" state).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.