Solved

Acrobat: Advanced Radio Buttons

Posted on 2004-09-09
13
945 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:goodmorals
  • 7
  • 6
13 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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?
0
 

Author Comment

by:goodmorals
Comment Utility
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.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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:

selectButton(1);

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

selectButton(2);


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.
0
 

Author Comment

by:goodmorals
Comment Utility
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?

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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;
    }
    else
    {
        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.
0
 

Author Comment

by:goodmorals
Comment Utility
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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.resetForm();

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?
0
 

Author Comment

by:goodmorals
Comment Utility
I uploaded the file to:

www.goodmorals.org/acrobat

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.
0
 

Author Comment

by:goodmorals
Comment Utility
I have 6.0.1 Professional.
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:goodmorals
Comment Utility
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"?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Acrobat’s JavaScript is a great tool to extend the application, or to automate recurring tasks. There are several ways a JavaScript can be added to the application or a document (e.g. folder level scripts, validation scripts, event handling scripts,…
Can Be Caused By Disabled Services I have encountered a problem viewing PDF files using Adobe Acrobat Reader.  For the longest time, PDFs might launch or might not.  Sometimes they took about 15 minutes to appear after launching them. After som…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now