5 Replies Latest reply on Aug 10, 2017 1:47 PM by try67

    If then statement in javascript

    kmreed01

      I am trying to get my text field "Text 2" to populate based on the value in field "Text 1".  If "Text 1" is less than 0.75, then "Text 2" should fill with "Not Met."  Below is the custom calculation that I am setting up for "Text 2."  Can someone tell me what I am doing wrong in my script?

       

      var a=this.getField("Text 1").value;

      var num1=0.75

      var num2=1.75

      var num3=2.75

      if(var a<var num1){

      event.value=a=="Not Met"?"":a;

      }

       

      Thank you!!

        • 1. Re: If then statement in javascript
          try67 MVP & Adobe Community Professional

          The "var" keyword should only be used once, to declare a variable for the first time. After that you only need to use the variable's name.

          So change this line:

          if(var a<var num1){

          To:

          if(a<num1){

          Also, this line is incorrect:

          event.value=a=="Not Met"?"":a;

           

          Replace that entire section of the code with just this:

          event.value= (a<num1) ? "Not Met" : "";
          
          • 2. Re: If then statement in javascript
            kmreed01 Level 1

            Thank you!!  I made the changes and it was accepted but the field remains

            blank.  The value in "Text 1" is 0.25714 but "Text 2" is blank.  Is it

            because "Text 1" is a calculated field?  Also, to finish out my script

            would the below be correct?

             

            var a=this.getField("Text 1").value;

            var num1=0.75

            var num2=1.75

            var num3=2.75

            if(a<num1){

            event.value= (a<num1) ? "Not Met" : "";

            }

            else if(a<num2){

            event.value= (a<num2) ? "Approaching" : "";

            }

            else if(a<num3){

            event.value= (a<num3) ? "Solid Performance" : "";

            }

            else if(a>num){

            event.value= (a>num3) ? "Exceeds Expectations" : "";

            }

             

            I really appreciate your help try67.  I've done a lot of studying

            • 3. Re: If then statement in javascript
              try67 MVP & Adobe Community Professional

              If you're using multiple conditions you can't combine it with the tertiary comparison operator.

              Instead, just to this:

              if (a<num1) event.value = "Not Met";

              else if (a<num2) event.value = "Approaching";

              else if (a<num3) event.value = "Solid Performance";

              else event.value = "Exceeds Expectations";

              • 4. Re: If then statement in javascript
                kmreed01 Level 1

                Thank you.  I have tried the below in the Custom Calculation Script and

                then in the Custom Format Script.  For some reason "text 2" remains blank

                even though there is a value in "text 1."

                 

                var a=this.getField("Text 1").value;

                 

                var num1=0.75

                 

                var num2=1.75

                 

                var num3=2.75

                 

                if (a<num1) event.value = "Not Met";

                 

                else if (a<num2) event.value = "Approaching";

                 

                else if (a<num3) event.value = "Solid Performance";

                 

                else event.value = "Exceeds Expectations";

                • 5. Re: If then statement in javascript
                  try67 MVP & Adobe Community Professional

                  - It's a Calculation script, not a Format script.

                  - Check the JS-Console (Ctrl+J) for error messages.