6 Replies Latest reply on Jan 19, 2017 7:09 AM by 123WorkingGuy

    Script for choosing radio button based on value

    123WorkingGuy

      Hi all,

       

      I have no experience in creating scripts. I have two scripts that I actually need (I think).

       

      The first script I would like/need is a on a field that sums up a series of totals for a grand total but I would like to have in brackets the percentage (as shown in the example below in red). The way it's laid out by the person who is wanting the form, is to have the grand total added up but I wanted to have that number than divided by the maximum score to get the percentage in brackets beside the total.

       

      My second script (if it's doable) would be based on that percentage to automatically choose/check one of the radio buttons below based on the calculated percentage from the grand total line as calculated above.

       

      If this is not clear please don't hesitate to ask questions as I will try to better explain myself.

       

      I am working in Adobe Acrobat Pro DC

       

      Thanks in advance.

      Paul

        • 1. Re: Script for choosing radio button based on value
          try67 MVP & Adobe Community Professional

          I recommend you do the percentage value in a separate field. It will make things easier.

          You could then combine both tasks into a single calculation script, like this:

           

          var grandTotal = Number(this.getField("GrandTotal").value);
          var gtPerc = grandTotal / 150;
          if (gtPerc<0.5) this.getField("OverallRating").value = "1";
          else if (gtPerc<=0.7) this.getField("OverallRating").value = "2";
          else this.getField("OverallRating").value = "3";
          event.value = "(" + (gtPerc*100).toFixed(2) + "%)";
          

           

          Replace the field-names and check-box export values with the actual ones in your form, of course.

           

          Edit: Fixed the last line of the code to match your desired format.

          • 2. Re: Script for choosing radio button based on value
            123WorkingGuy Level 1

            Thank you try67 for your quick response.

             

            I will do the percentage in a separate field beside the Grand Total field.

             

            But as I know little about scripting and related matters I have to ask where this script goes. I take it that the 'OverallRating' gets changed to the three radio button choices (Below Standard, Standard and Superior as I have labeled those three buttons that way? Like this?

             

            1. var grandTotal = Number(this.getField("GrandTotal").value); 
            2. var gtPerc = grandTotal / 150
            3. if (gtPerc<0.5) this.getField("Below Standard").value = "1"
            4. else if (gtPerc<=0.7) this.getField("Standard").value = "2"
            5. else this.getField("Superior").value = "3"
            6. event.value = "(" + (gtPerc*100).toFixed(2) + "%)"

             

            Not sure what you mean by 'check-box export values' or where to put this script to have the radio buttons automatically selected based on the percentage calculated.

             

            Thanks

            Paul

            • 3. Re: Script for choosing radio button based on value
              try67 MVP & Adobe Community Professional

              This script should be placed as the custom calculation script of the percentage field.

               

              Instead of creating three separate fields I recommend you name them all the same (I used "OverallRating" in my code, but it could be something else), and give each one a unique value (it could be "1", "2", "3", like in my code, or it could be "Below Standard", "Standard" and "Superior"). The fields can be either check-boxes or radio-buttons, it doesn't matter.

              • 4. Re: Script for choosing radio button based on value
                123WorkingGuy Level 1

                Hi try67,

                 

                Thanks sooo much. Was able to to get it to work. I created the separate field, labeled it 'Percentage', added the script in the 'custom script' box on the 'Calculate' tab. Tweaked the Radio Button's name to 'OverallRating' and set each button's choice to separately to 1, 2 or 3, made sure the script reflected those names and values. When I entered the values in the various fields the script ran and chose the appropriate radio button based on the total but no value was put in the 'Percentage' field and after enter values in the various fields I kept getting this response:

                I did figure out that the format for the 'Percentage' field was to be left at 'none' instead of 'percentage'. Probably the fact that I don't completely understand scripting.

                 

                May I ask one more question. In the other fields (that get added up) the person is suppose to enter a value from 1 to 5 or 1 to 10. Is there a way to limit/restrict someone entering a number outside of that range?

                 

                Many thanks for all your help.

                Paul

                • 5. Re: Script for choosing radio button based on value
                  try67 MVP & Adobe Community Professional

                  Sure, it's easily done. Set their Format option to Number and then you'll be able to set a valid range under the Validation tab.

                  • 6. Re: Script for choosing radio button based on value
                    123WorkingGuy Level 1

                    Got it. Thanks again try67.