Acrobat form javascript display text in one field based on value of other fields

New Here ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

Hello to everyone, and thanks in advance for any help.

In my pdf form, I have 8 fields that are answers to questions -- each field has a drop of yes or no. I've given yes the value of 1 and no the value of 2.

There is a 9th field (Advice Document) -- it is supposed to display text based on the yes/no replies to the 8 questions. Plus, it should display default text before any questions are answered or if a condition is not met.

Here is a screengrab of the field layout:

 

karenl32644045_0-1656037637487.png

 

This is what I've come up for code:

=======

var q1 = (this.getField("Q1").value);
var q2 = (this.getField("Q2").value);
var q3 = (this.getField("Q3").value);
var q4 = (this.getField("Q4").value);
var q5 = (this.getField("Q5").value);
var q6 = (this.getField("Q6").value);
var q8 = (this.getField("Q8").value);

if ( q1 = 1) event.value = "Text1" ;
else if ( q2 = 1) event.value = "Text2" ;
else if ( q3 = 2) event.value = "Text3" ;
else if ( (q4 = 1) && (q1 = 1)) event.value = "Text4" ;
else if ( q5 = 2) event.value = "Text3";
else if ( q6 = 1) event.value = "Text3" ;
else if ( q8 = 1) event.value = "Text5" ;
else if ( q8 = 2) event.value = "Text2" ;
else event.value = "DefaultText" ;

=========

(note: Q7 isn't referenced in the code because clicking either yes or no should display the default text)

 

No errors are flagged, but this doesn't work.

As soon as I close the field dialogue, "Text1" appears in the Advice Document field. Selecting yes or no for any other field has no effect.

 

I'd be very grateful to anyone who can point out what I'm doing wrong! (Many things, I suspect.)

 

Thank you.

 

TOPICS
JavaScript , PDF forms

Views

61

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

Replace

if ( q1 = 1 )

with

if ( q1 == 1 )

Change also the other if

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

Thanks for your help, Bernd. I changed all the =  to == in the ifs, but it didn't work. The result was the same, ie, As soon as I close the field dialogue, "Text1" appears in the Advice Document field. Selecting yes or no for any other field has no effect.

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

Where does you use the script?

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

You need to place 'if' and 'else if's' differently.

In your script when 'Q1' is 1, value will be "Text1" and when that happen rest of else if's won't trigger because first if is true, so you have to reorder them, for example, try first Q8, then Q7...etc.

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 ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

LATEST

Thanks Nesa, your comment about the first 'true' stopping the script was helpful; I hadn't considered that! 
However, I have reworked it so that each Q has its own result. That is working, from a technical point of view (and I'm using == not = for 'value', as Bernd suggested). Now just to see if the job owner is okay with it!
FYI, each Q is now structured like this:
event.value = (this.getField("yn1").value == 1) ? "Text1" :
(this.getField("yn1").value == 2) ? "Default Text" : "";

Thanks for the feeback, it helped.

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