Expand my Community achievements bar.

Javascript: if statements with multiple conditions

Avatar

Level 1

Hello,

I am a javascript novice looking to write code that is somewhat over my head.  I have some understanding of if, else statements - and not much else.

My form is organized similar to this with radiobuttons:

1: Yes/No    A: t,u,v

2: Yes/No    B: w,x,y

3: Yes/No

I have been able to script so that if 1 is yes, A is visible.  Then choosing A=t, one row of a table appears; u, two rows; etc.  2 is similar, and 3 is just a yes/no to visualize some other fields, with no additional radiobuttons.

I also wanted to add a check box to view the entire form if checked.  But when unchecked, I also want the fields to continue to show up as dictated by the selections for 1, 2, 3, A, and B.

If this is possible, I need help organizing the if/else statements so that the javascript will run.  My thoughts was to do:

If viewall checkbox ==checked, all visible

else{

     if (1 == Yes) {

          if (A == null) { fields=visible/invisible}   

          if (A == t) { fields=visible/invisible}

          if (A == u) {fields=visible/invisible}

          ...

     }

     if (2 == Yes) {

          if (B == null) {fields=visible/invisible}

          if (B == w) {fields=visible/invisible}

          ...

     }

     if (3 == Yes) {fields=visible/invisible}

}

else{

     if (1 == No) {fields=visible/invisible}

     if (2 == No) {fields=visible/invisible}

     if (3 == No) {fields=visible/invisible}

}

else{

If (viewall checkbox == unchecked) {all invisible}

}

I must either have problems with my logic or have syntax issues because the code will not execute.  But if this works, then I believe I will also will run into the problem of executing commands for all of the different scenarios, ie if 1 is yes, 2 is yes, but 3 is no.  So I need some help on how to set up this script, and any other issues I may not recognize.

Let me know if you need any additional information.  Thanks for your help.

Thanks,

Kevin

6 Replies

Avatar

Level 5

Hi,

I think you are trying to make the code to complicated. As I understand it your form starts with the following checkboxes shown.

ViewAll

1

2

3

so you could add code to each of the click events of these which shows the other fields

viewAll checkbox code would look like

if ( viewAll == Yes)

{

a.presence = "visible";

b.presence = "visible";

c.presence = "visible";

}

else

{

a.presence = "hidden"

a.presence = "hidden"

a.presence = "hidden"

}

checkBox 1 would look like

if ( 1 = Yes}

{

a.presence = "visible";

}

else

{

a.presence = "hidden";

}

and so on

Then if you clicked checkBox 1 your form would look like

viewAll

1     a: t, u, v

2

3

Then for each radio button/checkBox in A you could add code on the click event to show what you need

something like this

On click of t radio button

show field/tablerows that should be shown by t

hide fields/tablerows that are shown by u and v

On click of u radio button

show field/tablerows that should be shown by u

hide fields/tablerows that are shown by t and v

If you think it would help you could upload a sample document ( acrobat.com or some other sharing site)  and I could try and code on to that and give you it back as a sample of how I would solve it
Hope this helps
Malcolm

Avatar

Level 1

Thanks for your response,

I originally set up the script as you described, but I was trying to go one step further.  The issue with that particular script is that if you were to view all fields and then complete portions of the form then unclick the view all fields, your result would be an empty form.  I would like to retain those fields as visible if possible.  I was attempting to use the above code as part of the change script of the viewall checkbox.

Thanks,

Kevin

Avatar

Level 5

Hi,

Would disabling the view all checkbox work?

I will continue investigating to see if I can solve the problem.

Regards

Malcolm

Avatar

Level 2

If I understand properly, you need a way to know which sections to hide (those portions not filled in) when the View All option is deselected.

Maybe exactly when you deselect the View All option, you could interrogate all the fields to determine which sections to keep visible, and to on/off the corresponding radiobutton. This could result in alot of processing for this benefit.

Avatar

Level 1

Hi, thanks for your help.  That is what I am trying to do.  But I have had some difficulty in organizing the script so that it will run and include all scenarios.  My attempt is in the originial post; however, I believe that there is also a pitfall of how to script if some buttons are clicked on and some off.  Perhaps there is a better way to set it up, I would like to retain this feature if possible.

Thanks,

Kevin

Avatar

Level 5

HI,

The only solution I can think of would be to change the else of the viewAll script so that it is something like this

if ( viewAll == 1)

{

     //show all fields

}

else

{

     for ( each field in form)

     {

          if ( field.value == null)

          {

               field.presence = "hidden";

          }

     }

}

This way you would check if the field had been filled in and if so you don't hide it. you could make this easier by wrapping all linked questions in a subform and if any part of that subform is filled in do not hide the subform, if not then hide it.

Hope this helps

Malcolm