Skip to main content
Participant
August 10, 2017
Question

If then statement in javascript

  • August 10, 2017
  • 1 reply
  • 593 views

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

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
August 10, 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" : "";

kmreed01Author
Participant
August 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

try67
Community Expert
Community Expert
August 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";