2 Replies Latest reply on Feb 21, 2014 1:23 PM by meljar

    Hide/Show fields based on textbox

    meljar

      I'm just wondering if there is a way to hide certain fields based on whether a user enters a value into a text box and then reappear if that value in the text box is deleted and the field becomes blank again.

       

      I have two scenarios where I would like this to occur

       

      1. I have a checkbox "Not Recorded" and a text field "Blood Pressure". If the user checks the "Not Recorded" box, the Blood Pressure field will be hidden (I have managed this already). If the user unclicks the box, the Blood Pressure field reappears (again, I have done this already). Alternatively, if the user enters a value in "Blood Pressure" I would like the "Not Recorded" field to disappear. I have managed this as well with this code: (placed in the Action, Mouse Up, event javascript screen)

       

      this.getField("Not Recorded").display = event.target.value = "Off" ? display.visible : display.hidden;

       

      I'm not sure if this is the appropriate script that I should be using, but it did hide the "Not Recorded" field. The problem is, I would like the "Not Recorded" to be hidden if a value is entered into the Blood Pressure field and reappear if that value is then deleted (i.e., the user changes their mind or made a mistake and wants to click Not recorded instead). I'm not sure if this is doable or just way too complicated for me to do.

       

      2. In the second scenario, I have 2 text fields where the user needs to enter a value for Weight in either lbs or kgs. There is a text box for a value in lbs and a text box for a value in kgs. If the user enters in a value for lbs, I would like the field for kgs to be hidden, but again, if they delete that value so that the lbs field becomes blank, I would like the field for kgs to reappear.

       

      is this doable or is there another way I might be able to do this?

        • 1. Re: Hide/Show fields based on textbox
          George_Johnson MVP & Adobe Community Professional

          For the first one, I would not bother hiding the check box, for the reason you give. In addition to hiding the text field, it should reset it so that any value that the user may have entered is not retained when it is hidden.

           

          If you really want to show/hide the check box based on whether there's an entry in the text field, you could use the following custom Validate script for the text field:

           

          var f = getField("Not Recorded");

          if (!event.value) {

              f.display = display.visible;

          } else {

           

             f.value = "Off";

             f.display = display.hidden;

          }

           

          For this script to start working, you will have to change the value in the text field.

           

          For the second problem, what I do is have a single text field and two check boxes to indicate lbs or kgs. If you want to stay with the setup you have, you can clear the other text field in with a custom validate script:

           

          // Custom validate script for weight_lbs field

          if (event.value) getField("weight_kgs").value = "";

           

          Do the same for the other text field but change the field name in the script. This will allow only one field to be filled-in at a time and you don't have to bother with showing/hiding.

          • 2. Re: Hide/Show fields based on textbox
            meljar Level 1

            Thanks! that works perfectly!