4 Replies Latest reply on Jan 6, 2009 12:44 PM by (Francis_Auger)

    Acrobat Forms - javascript to hide/unhide text box if any one of a number of radio buttons is enable

    Level 1
      I have a form which I am designing in Acrobat 9 which has a section where there are three questions, each with a pair of radio buttons - one for 'yes' and one for 'no'. They are in the style of a check box (with a tick). I need some help to write some code which will unhide a single field (or multiple fields if I need to use something similar at a later date) if any one of the 'yes' radio buttons is checked. So if all three radio buttons were checked, the field would be hidden but if you then changed your mind and clicked a yes button, the field would be visible.

      I am new to this so any help would be greatly appreciated. Many thanks in advance! I hope that i have explained this clearly btw.
        • 1. Re: Acrobat Forms - javascript to hide/unhide text box if any one of a number of radio buttons is enable
          (DSmith6630) Level 1
          I think you meant to say "So if all three radio buttons were checked [no], the field would be hidden but if you then changed you mind and click a yes button, the field would be visible" I guess the field would also be visible if the buttons were not checked.

          So, let's give it a try.
          Let's say you name the three sets of radio buttons rb1, rb2 and rb3. The value for a Yes answer is set to "Y" and the value for a No answer is set to "N".
          Let's say the text box you want to hide is called textbox.
          On the MouseUp event on each one of the radio buttons (yes AND no), you should enter the following code:
          if((getField("rb1").value=="Y")||(getField("rb2").value=="Y")||(getField("rb3").value=="Y" ))
          {
          getField("textbox").display=display.hidden;
          //you could add the other fields you mentioned here later
          }
          else
          getField("textbox").display=display.visible;

          Hope this helps.
          • 2. Re: Acrobat Forms - javascript to hide/unhide text box if any one of a number of radio buttons is enable
            Level 1
            Thank you for your gracious help - I did finally figure it out not longer after I posted this and have only just remembered that I posted this to get some help. I did it another way but I am going to save this page (to pdf of course) and keep it as reference code as I have a brain like a sieve!

            I actually assigned numerical values to the radio buttons eg 1 for the Yes boxes and 0 for the no boxes. If the value was greater or equal to one the text field was made visible. I used it on the caluclate script (on the hidden text field) so even when no boxes were checked the field remained hidden. I know that you have to be careful with having too many caluclate fields and it may not be thes best way of doing it but it worked so I was happy. I will dig out the forms and post the code if anyone would like to say but I think my explanation is simple enough!

            Thanks again DSmith6630!
            • 3. Re: Acrobat Forms - javascript to hide/unhide text box if any one of a number of radio buttons is enable
              gkaiseril MVP & Adobe Community Professional
              For ease of maintenance you might want to make the above script into 2 doucment level JavaScript function. One could set a return value to the result of the caparison and the other could show/hide the text field based on the result. Then a call to the show/hide function could control the show and display of the field(s) and and a call to the function for the results of the comparison could be used anywhere else and not change the field display property.

              funciton Test3RB() {
              // set logical result based on result of test
              var bReslut = ((this.getField("rb1").value == "Y")|(this.getField("rb2").value == "Y") |(this.getField("rb3").value == "Y"));
              return bResult; // return value of logical test
              } // end funciton

              function ShowText() {
              // assume field not to be shown
              this.getField("textbox").display=display.hidden;
              if (Test3RB() ) {
              this.getField("textbox").display=display.visible;
              } // end if true
              return;
              } // end ShowText function

              You mouse up action for each button could be:

              ShowText();

              And if you could test the logical value returned by the function somewhere else and did not want to modify the display field:

              if(Test3RB() ){
              // action for true
              } else {
              // action for false
              }