Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

If then statement in javascript

New Here ,
Aug 09, 2017 Aug 09, 2017

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!!

TOPICS
Acrobat SDK and JavaScript
544
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2017 Aug 09, 2017

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" : "";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 10, 2017 Aug 10, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 10, 2017 Aug 10, 2017

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";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 10, 2017 Aug 10, 2017

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";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 10, 2017 Aug 10, 2017
LATEST

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines