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

New Here ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

145

Likes

Translate

Translate

Report

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

correct answers 1 Correct Answer

Adobe Community Professional , Aug 20, 2018 Aug 20, 2018
Also, change the first two lines to:var a = Number(this.getField("adults").value);var b = Number(this.getField("children").value);

Likes

Translate

Translate
LEGEND ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

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 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

Use this:

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

event.value = "FSR";

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

event.value = "FSR";

else

event.value = "PAU";

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

Also, change the first two lines to:

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

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

Likes

Translate

Translate

Report

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 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

LATEST

thank you, that worked.

Likes

Translate

Translate

Report

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
LEGEND ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
LEGEND ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

Ok, so how about the JavaScript console?

Likes

Translate

Translate

Report

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 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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

I did try that and still nothing happens.

Likes

Translate

Translate

Report

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
LEGEND ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

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