9 Replies Latest reply on Sep 10, 2014 4:52 PM by berocca

    Pre-filling a text box from a Radio Button selection

    berocca Level 1

      Hi folks,

       

      I've just noticed an error in one of my earliest attempts at Javascript - luckily I don't think any of my colleagues have noticed it yet, but I need to fix it asap!

       

      I have two RadioButtons naned 'Radio Button13'. One is for when the person is Full-Time and one is for when they are Part-Time. When the Full-Time button is selected, I get text (which I have placed in the Button Value) to pre-fill a text box below. The text is something along the lines "38 hours per week". Part-Time has nothing.

       

      In the Mouse Up script for Full-Time I have placed:

       

      if (this.getField("Yes").value == "1")

      this.getField("Working Hours").value = "8:30am to 5pm Monday to Friday "  <<<This text isn't even the same as the button value so I don't know why I put that there, that is not what I want in the text box.

       

       

      The text from the Button value filters through to the Text box (called "Working Hours") fine, but the idea is that when Part-Time is selected, the user is able to type the hours into the box. You can type, but as soon as you exit the text box, the text vanishes.

       

      Where have I gone wrong? How can I fix it?

        • 1. Re: Pre-filling a text box from a Radio Button selection
          George_Johnson MVP & Adobe Community Professional

          It sounds like you also have a custom calculation script set up for the Working Hours field. Is that right?

          • 2. Re: Pre-filling a text box from a Radio Button selection
            berocca Level 1

            Oops, yes I do. It is:

             

            var v = this.getField("Radio Button13").value;

            event.value = (v=="Off") ? "" : v;

            • 3. Re: Pre-filling a text box from a Radio Button selection
              George_Johnson MVP & Adobe Community Professional

              OK, it's that script that overrides the one in the radio button. Since it always sets a new value, even when you attempt to manually change the field value, you get the behavior you're seeing. Calculation scripts are triggered whenever any field value changes, so when you change the value of the text field, the script is triggered and the field value gets changed by the script.

               

              You're mixing up the two general approaches you can take to deal with this, the first being to use Mouse Up scripts in the radio buttons to control the text fields, and the second being a calculation script in the text field to set it's value. There are good reasons to choose one over the other, and both can accomodate the functionality you want. But something doesn't make sense to me. That calculation script should set the value of the text field to the button value of the part time radio button as well. Is that's what's happening?

              • 4. Re: Pre-filling a text box from a Radio Button selection
                berocca Level 1

                Hey George, I had another look and saw that the Part-Time button value is "space" - like, a spacebar space. Guessing this is wrong!?

                • 5. Re: Pre-filling a text box from a Radio Button selection
                  George_Johnson MVP & Adobe Community Professional

                  It's not wrong, but it does answer my question. If you keep things set up the way they are now, you could use the following custom calculate script:

                   

                   

                  (function () {

                   

                      // Get a reference to the radio button group

                      // and its value

                      var f = getField("Radio Button13");

                      var v = f.valueAsString;

                     

                      // Allow the user to enter text

                      event.target.readonly = false;

                     

                      // Only set this field's value if a change in the radio button

                      // is what triggered this script

                      if (event.source && event.source === f) {

                     

                          if (v !== "Off" && v !== " ") {

                              event.target.readonly = true;

                              event.value = v;

                          } else {

                              event.value = "";

                          }

                         

                      }

                   

                  })();

                  • 6. Re: Pre-filling a text box from a Radio Button selection
                    berocca Level 1

                    Yes! That's perfect! Thank you, you definately saved my skin! Luckily, we hadn't had any Part-Time employees use this form yet haha.

                    • 7. Re: Pre-filling a text box from a Radio Button selection
                      George_Johnson MVP & Adobe Community Professional

                      In case it wasn't clear, you should also get rid of any of the Mouse Up scripts in the radio buttons.

                      • 8. Re: Pre-filling a text box from a Radio Button selection
                        berocca Level 1

                        Hi,

                         

                        I am used this same code in another form where I want to pre-fill a text box based on a radio button selection. ie. when the button with a value of " " is ticked nothing is pre-filled and the user writes their own text. When the button with the value "No" is ticked, some text appears in the text box.


                        It works fine for this purpose, however I need to be able to add to the pre-filled text and it does not allow me to do this. Is there anything I can do?

                         

                        (function () {

                         

                            // Get a reference to the radio button group

                            // and its value

                            var f = getField("Outcome2A2");

                            var v = f.valueAsString

                            // Allow the user to enter text

                            event.target.readonly = false;

                            // Only set this field's value if a change in the radio button

                            // is what triggered this script

                            if (event.source && event.source === f)

                                if (v !== "Off" && v !== " ") {

                                    event.target.readonly = true;

                                    event.value = "Blah blah blah";

                                } else {

                                    event.value = "";

                         

                                }

                         

                            }

                         

                        })();

                         

                        Thanks.

                        • 9. Re: Pre-filling a text box from a Radio Button selection
                          berocca Level 1

                          Well, I think I have just answered my own question. It's as simple as removing the "readonly" bit isn't it? :-P