Skip to main content
Participant
April 29, 2020
Question

How to combine show / hide and If-else conditions in a Text Field based on Checkbox Selections

  • April 29, 2020
  • 1 reply
  • 796 views

I am working on a pavilion rental form and having trouble with code in one specific section.

 

There is a "total" text field that will reflect a dollar value. The value is determined by which checkbox "pavilion" a user wants to rent. The if-else conditions I created for this work as I intend.

 

event.value = 0;
if (this.getField("Roller Hockey").value!="Off") event.value=53;
else if (this.getField("Tot Lot").value!="Off") event.value=53;
else if (this.getField("VP Large").value!="Off") event.value=80;
else if (this.getField("Submarine").value!="Off") event.value=53;
else if (this.getField("Mermaid").value!="Off") event.value=53;
else if (this.getField("TSC Large").value!="Off") event.value=80;
else event.value = "0";

 

However, there are separate checkboxes for the time period a person wishes to rent, since they are booked in half-day periods. I want the "total" text field and value from the if-else conditions to show or hide based on which time period is selected. 

 

if (this.getField("8 AM – 12 PM").value != "Off") {
this.getField("Total1").hidden = false;
} else {
this.getField("Total1").hidden = true;
}

 

I can get both codes to work individually, but I don't know how to combine them so they can run together. I am fairly new to JavaScript and learning as I go along. Any help would be greatly appreciated.

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
April 29, 2020

Just put them all in the same script... However, you can change the reference from his.getField("Total1") to event.target in that case. Also, you should not use the hidden property, as it was deprecated a while ago. Use the display property, instead.

Participant
May 4, 2020

When I put them all in the same script it doesn't work. In the case of the script below - the "Total1" field remains hidden regardless of whether the checkbox is selected or not. But as I said - I am a complete newbie to this. Most of what I've taught myself is just from trying to rework examples and explanations from other posts/pages I've found. I'm assuming when I'm putting it together I'm doing something entirely wrong in the script. Did I even come close to what you were trying to explain? See below:

 

if (this.getField("8 AM – 12 PM").value != "Off") {
event.target.display = false;
} else {
event.target.display= true;
}

 

event.value = 0;
if (this.getField("Roller Hockey").value!="Off") event.value=53;
else if (this.getField("Tot Lot").value!="Off") event.value=53;
else if (this.getField("VP Large").value!="Off") event.value=80;
else if (this.getField("Submarine").value!="Off") event.value=53;
else if (this.getField("Mermaid").value!="Off") event.value=53;
else if (this.getField("TSC Large").value!="Off") event.value=80;
else event.value = "0";

 

try67
Community Expert
Community Expert
May 4, 2020

The "display" property is not a boolean. Change the first part to:

 

if (this.getField("8 AM – 12 PM").value != "Off") {
event.target.display = display.hidden;
} else {
event.target.display= display.visible;
}