• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Re: Calculations and lots of Conditionals

# Calculations and lots of Conditionals

Community Beginner ,
Oct 09, 2020 Oct 09, 2020

Copied

Howdy, knowledgeables!

I currently have eight tabs open on my browser in my effort to crack this code, and not one of them has helped me accomplish a simple, singular if/else effectively in a particular box's properties, which doesn't bode well for the fact that the ultimate if/else should be many, many lines tall.

What I need to create is three auto-filling field variations, two of which are not mathmatically derived but are numerical values unique from but reliant on the user-entered value of another field, while the third field IS the mathematical sum of the two auto-generated fields, but only if a checkbox is marked. At this point, I would very much just appreciate hunks of text I can copy and drop where they need to go!

So in other words, there are five total fields and one checkbox interacting in the chain: if the user enters into field "A" a value of "1", field "B" will automatically display a value of "3"; elsewhere, if the user enters into field "C" a value of "9," field "D" will automatically display a value of "7"; the final field "E" will be equal to the value of field "B", UNLESS checkbox "F" is selected, which should cause the final field to equal the sum of the values in field "B" and field "D".

what I've spent the past five hours troubleshooting variations of for my very first step in this series is this code, within the custom calculation property of field "B":

``````var A = this.getField( "A").value;
if( A = 1) event.value = -5;
else if( A = 2) event.value = -5;
else if( A = 3) event.value = -4;
else if( A = 4) event.value = -4;
else event.value = 0;``````

What I get instead of the number associations desired is, whether the value of "A" is entered as 1, 3, or 35, field "B" only displays the last event.value that ISN'T zero--even if I reduce it to a singular "if" and "else". I've tried it with brackets, indentations, and removed/added spaces in an untold number of combinations, but my resulting "last non-zero event.value" doesn't change.

So as you can see from the above sample, the value I need displayed in "B" is not mathmatically derived from the user-entered value in "A", which will be the case with fields "C" and "D", albeit with different number associations, hence the need for if/else. What, then, should I actually be entering?

...And should you brilliant minds have an answer for my uneducated self in this, I can already tell you what my NEXT question will be in regards to field "E": how do I designate a checkbox's selected status into the conditional? I can't find a logical consensus.

TOPICS
Acrobat SDK and JavaScript

Views

648

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 Expert , Oct 09, 2020 Oct 09, 2020

I forgot to add that, your script above is not working because you're not using the double equal comparison operator..

See the link below to understand better  the differences between =, ==. ===:

So to make that script in "B" field work you need to use it like this:

``````event.value = 0;

if(this.getField("A").value !=="") {

if(this.getField("A").value == 1) event.value = -5;

if(this.getField("A").value== 2) event``````
...

5 Replies 5
Community Expert ,
Oct 09, 2020 Oct 09, 2020

Copied

You can use something like this:

``````//custom calculation script of field B

if (this.getField("A").value !=="") {
if (this.getField("A").value ==1) event.value = 3;
} else {
event.value = event.target.value;
}

//custom calculation script for field D

if (this.getField("C").value !=="") {
if (this.getField("C").value ==9) event.value = 7;
} else {
event.value = event.target.value;
}

//custom calculation script for field E

if (this.getField("F").value !=="Off") {event.value = this.getField("B").value+this.getField("D").value;
} else {
if (this.getField("F").value =="Off") event.value = this.getField("B").value;
}``````

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 Expert ,
Oct 09, 2020 Oct 09, 2020

Copied

I forgot to add that, your script above is not working because you're not using the double equal comparison operator..

See the link below to understand better  the differences between =, ==. ===:

So to make that script in "B" field work you need to use it like this:

``````event.value = 0;

if(this.getField("A").value !=="") {

if(this.getField("A").value == 1) event.value = -5;

if(this.getField("A").value== 2) event.value = -5;

if(this.getField("A").value== 3) event.value = -4;

if(this.getField("A").value== 4) event.value = -4;

}``````

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 Beginner ,
Oct 15, 2020 Oct 15, 2020

Copied

This worked brilliantly! Curiously, the first solution you provided (from the other comment) resulted in more of the same, where the value entered in A didn't cause any change in the value of field B. I am curious why that might be, but I have no expectation for you to go out of your way and break it down for a casual like myself. (;

Thanks too for the link! I had noticed in some of my eight running tabs that "==" had been used, so that was part of my variation-testing; knowing WHY it mattered, and where, helps tremendously for future trouble-shooting, lol!

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 Expert ,
Oct 15, 2020 Oct 15, 2020

Copied

LATEST

``else event.value = 0;``

That is why I moved this line to the top of the second script:

``event.value = 0;``

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 Beginner ,
Oct 13, 2020 Oct 13, 2020