Skip to main content
Participant
August 20, 2018
Answered

I am trying to get a field to display a string depending on an if statement

  • August 20, 2018
  • 3 replies
  • 1006 views

I have 3 text boxes involved in this code. the first text box is called adults, the second is called children and the third is called result. Once the user enters numbers into the first 2 textboxes the 3rd should display a result, and the result will be different depending on the numbers entered in the adults and children text boxes. here is the code that I wrote for it but it isn't working.

var a = this.getField("adults").value;

var b = this.getField("children").value;

if ((a == 1) && (b >= 5))

(event.value == "FSR")

else if ((a <= 2) && (b >= 8))

(event.value == "FSR")

else

(event.value == "PAU")

I have this code written in the "custom calculation" area of the result text box properties.

This topic has been closed for replies.
Correct answer try67

Use this:

if ( a==1 && b>=5 )

event.value = "FSR";

else if ( a<=2 && b>=8)

event.value = "FSR";

else

event.value = "PAU";


Also, change the first two lines to:

var a = Number(this.getField("adults").value);

var b = Number(this.getField("children").value);

3 replies

Legend
August 20, 2018

Ok, so how about the JavaScript console?

Participant
August 20, 2018

you mean, did I try having the code run on a mouse event?

I did try that and still nothing happens.

Inspiring
August 20, 2018

You can use the key combination of <Ctrl> + J.

Your console will show nothing. You are using the wrong operator for the assignment of a value.

Inspiring
August 20, 2018

JavaScript has an "assignment" operator and 2 equality comparison operators and they are not interchangeable.

"=" is the assignment operator and used to assign a value to an object or variable.

Assignment Operators

"==" is the comparison operator and performs type conversion for the comparison.

"===" is the strict comparison and does not perform type conversion for the comparison.

Type conversion is usually converting Strings to Numbers.

Comparison Operators

Try the following corrected code in the Custom JavaScript code" calculation:

var a = this.getField("adults").value;

var b = this.getField("children").value;

if ((a == 1) && (b >= 5));

(event.value = "FSR");

else if ((a <= 2) && (b >= 8));

(event.value = "FSR");

else

(event.value = "PAU");

Legend
August 20, 2018

What exact result do you expect, and what do you see? Looking at code we can't always deduce what you intended, only what it actually does. What errors if any do you see in the JavaScript console?

Participant
August 20, 2018

I expect for either the letters "FSR" or "PAU" to show up in the result text box.

what I see right now when I enter numbers into the "adults' and "children" text boxes is nothing. the "result" text box remains empty.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 20, 2018

Use this:

if ( a==1 && b>=5 )

event.value = "FSR";

else if ( a<=2 && b>=8)

event.value = "FSR";

else

event.value = "PAU";


Also, change the first two lines to:

var a = Number(this.getField("adults").value);

var b = Number(this.getField("children").value);