• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Javascript to make fields visible or hidden depending on value of another field

Community Beginner ,
Jan 29, 2020 Jan 29, 2020

Copy link to clipboard

Copied

Hi, I am a novice at scripting.

 

I have the following fields:

 

"MOT No Vehicles" – allows input of values between 1 and 25

"MOT CH 1" … to "MOT CH 25"

"MOT CH Type 1" … to "MOT CH Type 25"

"MOT CH Days 1" … to "MOT CH Days 25"

"MOT CH Head 1" … to "MOT CH Head 25"

Depending on the value of "MOT No Vehicles", these fields should either be visible or hidden.

For example, if "MOT No Vehicles" = 10 then fields "MOT CH …1" to  "MOT CH …10" will be visible and "MOT CH …11" to "MOT CH …25" will be hidden.

 

Currently I have assigned variables "z" to field "MOT No Vehicles" and "aa" to "yd" to the other fields,

I then have 25 if statements to determine whether to display is visible or hidden.  Using the above example, my code is shown below.  

This code works fine but is very cumbersome and there must surely be a more efficient way to do this.

 

Any help will be appreciated.

if (z.value === 15){
aa.display = display.visible;
ab.display = display.visible;
ac.display = display.visible;
ad.display = display.visible;
ba.display = display.visible;
bb.display = display.visible;
bc.display = display.visible;
bd.display = display.visible;
ca.display = display.visible;
cb.display = display.visible;
cc.display = display.visible;
cd.display = display.visible;
da.display = display.visible;
db.display = display.visible;
dc.display = display.visible;
dd.display = display.visible;
ea.display = display.visible;
eb.display = display.visible;
ec.display = display.visible;
ed.display = display.visible;
fa.display = display.visible;
fb.display = display.visible;
fc.display = display.visible;
fd.display = display.visible;
ga.display = display.visible;
gb.display = display.visible;
gc.display = display.visible;
gd.display = display.visible;
ha.display = display.visible;
hb.display = display.visible;
hc.display = display.visible;
hd.display = display.visible;
ia.display = display.visible;
ib.display = display.visible;
ic.display = display.visible;
id.display = display.visible;
ja.display = display.visible;
jb.display = display.visible;
jc.display = display.visible;
jd.display = display.visible;
ka.display = display.visible;
kb.display = display.visible;
kc.display = display.visible;
kd.display = display.visible;
la.display = display.visible;
lb.display = display.visible;
ld.display = display.visible;
ma.display = display.visible;
mb.display = display.visible;
mc.display = display.visible;
md.display = display.visible;
na.display = display.visible;
nb.display = display.visible;
nc.display = display.visible;
nd.display = display.visible;
oa.display = display.visible;
ob.display = display.visible;
oc.display = display.visible;
od.display = display.visible;
pa.display = display.hidden;
pc.display = display.hidden;
pd.display = display.hidden;
qa.display = display.hidden;
qb.display = display.hidden;
qc.display = display.hidden;
qd.display = display.hidden;
ra.display = display.hidden;
rb.display = display.hidden;
rc.display = display.hidden;
rd.display = display.hidden;
sa.display = display.hidden;
sb.display = display.hidden;
sc.display = display.hidden;
sd.display = display.hidden;
ta.display = display.hidden;
tb.display = display.hidden;
tc.display = display.hidden;
td.display = display.hidden;
ua.display = display.hidden;
ub.display = display.hidden;
uc.display = display.hidden;
ud.display = display.hidden;
va.display = display.hidden;
vb.display = display.hidden;
vc.display = display.hidden;
vd.display = display.hidden;
wa.display = display.hidden;
wb.display = display.hidden;
wc.display = display.hidden;
wd.display = display.hidden;
xa.display = display.hidden;
xb.display = display.hidden;
xc.display = display.hidden;
xd.display = display.hidden;
ya.display = display.hidden;
yb.display = display.hidden;
yc.display = display.hidden;
yd.display = display.hidden;
}
TOPICS
Acrobat SDK and JavaScript

Views

1.9K

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

Community Expert , Jan 29, 2020 Jan 29, 2020

You can use this code to do it all:

 

var v = Number(this.getField("MOT No Vehicles").valueAsString);
for (var i=1; i<=25; i++) {
	this.getField("MOT CH "+i).display = (i<=v) ? display.visible : display.hidden;
	this.getField("MOT CH Type "+i).display = (i<=v) ? display.visible : display.hidden;
	this.getField("MOT CH Days "+i).display = (i<=v) ? display.visible : display.hidden;
	this.getField("MOT CH Head "+i).display = (i<=v) ? display.visible : display.hidden;
}

 

Likes

Translate

Translate
Community Expert ,
Jan 29, 2020 Jan 29, 2020

Copy link to clipboard

Copied

You can use this code to do it all:

 

var v = Number(this.getField("MOT No Vehicles").valueAsString);
for (var i=1; i<=25; i++) {
	this.getField("MOT CH "+i).display = (i<=v) ? display.visible : display.hidden;
	this.getField("MOT CH Type "+i).display = (i<=v) ? display.visible : display.hidden;
	this.getField("MOT CH Days "+i).display = (i<=v) ? display.visible : display.hidden;
	this.getField("MOT CH Head "+i).display = (i<=v) ? display.visible : display.hidden;
}

 

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
Community Beginner ,
Jan 29, 2020 Jan 29, 2020

Copy link to clipboard

Copied

LATEST

Thanks, this works like a charm.  I have quite a few similar instances in my form so I will be able make my form much more efficient.

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