8 Replies Latest reply on Jul 10, 2014 2:54 PM by gkaiseril

    Why Doesn't My Code Work?

    Jared_Dawson

      I am trying to make it so if a user checks a radio button within a group of 2 (Choice1 & Choice2) the formula within the field "Total" changes.

       

      This is the javascript within the field "Total"

       

      //////////////////////////////////////////////////////////////////////////////

      if(document.Group1('Choice1').checked)

      {

      event.value = (this.getField("WSAtotal").value + this.getField("SAandAdd").value);

      }


      else if(document.Group1(Choice2').checked)

      {

      event.value = (this.getField("NSAtotal").value + this.getField("SAandAdd").value);

      }

      //////////////////////////////////////////////////////////////////////////////

       

      P.S. I don't know anything about javascript so I have just been Frankensteining this code from around the web.

        • 1. Re: Why Doesn't My Code Work?
          Bernd Alheit Adobe Community Professional & MVP

          You must use Acrobat Javascript not Javascript for Web Browsers.

          • 2. Re: Why Doesn't My Code Work?
            Jared_Dawson Level 1

            I looked through the API and some other threads, I am have a dickens of a time wrapping my head around this. I can do basic python and objective-c but javascript is alluding me.

             

            ///////////////////////////////////////////////

            if(Group1 = Choice1)

            {

            event.value = (this.getField("WSAtotal").value + this.getField("SAandAdd").value);

            }

             

             

            if(Group1 = Choice2)

            {

            event.value = (this.getField("NSAtotal").value + this.getField("SAandAdd").value);

            }

            ///////////////////////////////////////////////

            • 3. Re: Why Doesn't My Code Work?
              gkaiseril MVP & Adobe Community Professional

              First if you have radio buttons that should be in an exclusionary group, give them the same field name but different export values.

               

              Radio buttons are fields, so you first need to access the field object before testing the value of the field.

               

              var oGroup = this.getField("Group"); // get field object for radio buttons named "Group";

               

              // some debugging help;

              console.show();

              console.clear();

              console.println("Group field has a value of " + oGroup.value);

              // end debugging help;

               

              event.value = ""; // clear event value;

              if(oGroup.value == "Choice1") {

              // value for group object is "Choice1";

              event.value = (this.getField("WSAtotal").value + this.getField("SAandAdd").value);

              }

              if(oGroup.value == "Choice2")  {

              // value for group object is "Choice2";

              event.value = (this.getField("NSAtotal").value + this.getField("SAandAdd").value);

              }

              • 4. Re: Why Doesn't My Code Work?
                Jared_Dawson Level 1

                Hmm, I may be going about this wrong. I am adding a custom calculation script to a text field name "Total". If that is the case why do I need to declare variables if they already exist? Is that simply telling "Total" that somewhere  within the pdf there is a group of radio buttons named "Group1" and it has export values of "Choice1" and "Choice2", I don't want to go and re-create those through scripting, I just want to let "Total" now where to get values from?

                 

                Here is a picture of my pdf to maybe help better illustrate what I'm trying to do.

                Basically I want the user to be able to check the radio buttons (I made them look like checkboxes) and by doing so the "Total" field uses the total of the left or right column.

                Capture.PNG

                • 5. Re: Why Doesn't My Code Work?
                  gkaiseril MVP & Adobe Community Professional

                  Any errors in the JavaScript console?

                   

                  I can show you images of forms with calculations that work but without the full context of the form the image is pretty useless.

                  • 6. Re: Why Doesn't My Code Work?
                    Jared_Dawson Level 1

                    Holy balls I think it is working now (FYI change my radio button(s) name to Radio for clarity), I changed


                    var Radio = this.getField("radio");

                    to

                    var Radio = this.getField("Radio");


                    and it seems to be working now.

                    I'll need to do some more testing to make sure there are no calculation errors, but thank you for the help Bernd & GKaiseril.

                    • 7. Re: Why Doesn't My Code Work?
                      Test Screen Name Most Valuable Participant

                      The contents of fields don't automatically become variables. this.getField(fieldname) makes an object which refers to a field called "fieldname". Once you have this object you can start cooking, because you can both get and set the value of the field (and many other things, like change its colour, which is why you have to say if you are working with the value - this.getField(fieldname).value

                       

                      And in case you didn't pick this up, in languages derived from C (including C++, Java and JavaScript) the form

                       

                      if ( a = b ) is a huge trap, common error, and enormous source of problems. You must write

                      if ( a == b )

                       

                      The first form sets the value of a, and does the "if" thing if a is not zero, which is not what you want (but it is what you asked for!)

                      • 8. Re: Why Doesn't My Code Work?
                        gkaiseril MVP & Adobe Community Professional

                        JavaScript is case sensitive so you need to be very careful in naming objects.