4 Replies Latest reply on Dec 16, 2011 5:57 AM by LegendGraphics

    "this" not working in Function

    LegendGraphics Level 1

      I'm having trouble getting an "if" to run in a function.  I know the function is being called OK because I put some test app.alert's in and they work.  It just doen't like my "if" script, which runs OK as a standalone in each field.  Does it have something to do with "this"?

       

      12-14-2011 5-13-56 PM.jpg

      I'm calling it with:

       

      functionCB.exclusiveCB();

       

      Thanks,

       

      Wendell

        • 1. Re: "this" not working in Function
          Kevin Cavallo Level 4

          Wendell,

           

          If the function is in a scripting object, then 'this' is not in scope. You would want

           

          function exclusiveCB (cb) {

          if (cb.rawValue == 1)

               {

               .

               .

               }

           

          And call it from the event with: functionCB.exclusiveCB (this);

           

          However, I think there is a bigger problem.  It looks like you are trying to toggle off every other checkbox when one is checked.  Your script is not generic to be moved out of a field event, since it will always check checkbox4 and uncheck everything else.

           

          If you need exclusive checkbox behavior, try using a radio button list.  The exclusivity is handled for you.

           

          Kevin

          • 2. Re: "this" not working in Function
            LegendGraphics Level 1

            Hi Kevin,

             

            I would like to use the radio button function, but our forms are used by visually handicapped folk.  1.) I haven't figured out how to uncheck a radio button if the user changes their mind and doesn't want to check any button in the set.  Also, it's better for our purpose to let the user tab through all the choices rather than have to switch to the up and down keys.

             

            Actually, the script I loaded was not complete.  Indeed, I do not want checkbox 4 to constantly set.  The way it works (perfectly) when implanted in each checkbox field is,  if "this.rawValue == 1", all the checkboxes in the set, including the current checkbox, is set to zero.  Then, the script sets "this.rawValue = 1".  This way, it works the same in each field without editing. I just want to streamline the code and have never used functions.

             

            I tried the method you described, and the witness app.alert("head") works and then it dies. app.alert("foot") doesn't work.

             

            I'm calling this function with: 

             

            functionCB.exclusiveCB (this); 

             

            12-14-2011 6-51-46 PM.jpg

            • 3. Re: "this" not working in Function
              Kevin Cavallo Level 4

              Javascript is case-sensitive; your code needs to match the object names exactly.  Usually when you add a checkbox the default name is "CheckBox1".  Unless you've renamed them, make sure that your script uses the correct case of the control names.

               

              Also, put a semicolon (";") a the end of the app.alert lines.  Javascript is tolerant of missing semicolons, but it's good practice to always include them because in certain cases a missing one will cause weird behavior.

              • 4. Re: "this" not working in Function
                LegendGraphics Level 1

                Kevin,

                 

                The solution involved the "cb" change you suggested:

                 

                function exclusiveCB (cb) {

                if (cb.rawValue == 1)

                     {

                     .

                     .

                     }

                 

                And call it from the event with: functionCB.exclusiveCB (this);

                 

                And... even though "checbox1.rawValue" was enough reference when the code was run at field level, when run as a function, I had to expand the reference to:

                 

                Page1.checkbox1.rawValue = 0;

                 

                The form checkboxes work beautifully.

                 

                I really appreciate your help on this. 

                 

                Wendell