Hide and show multiple fields on a form based on conditions using Java Script.

  • 0
  • 1
  • Question
  • Updated 2 months ago
  • Answered
Below is my scenario:
I have 2 tables Course and Enrollment. 
Course table contains Course name, number of activities, assessment method(total 10 text fields) and course description. 
Enrollment contains 2 Pass or fail field (dropdown) and a date field for one assessment method field. 
So for every Assessment method field there are 2 passfail and date fields. (total 20)

Enrollment and course are connected in such a way that many enrollments belong to one course. 

I need to show or hide these pass fail and date fields based on number of activities field in course. When a course name is selected from a drop down, number of activities is also displayed as a lookup field. 

Example: for number of activities =3 
show Assesment method 1 , 2 and 3 
show pass fail 1 ,2,3,4,5 and 6
show date 1 ,2,3,4,5 and 6
hide assessment method 4 to 10
hide pass fail 7 to 20 
hide date 7 to 20. 

Can anyone please help to resolve this?
Photo of Raj Helaiya

Raj Helaiya

  • 1,550 Points 1k badge 2x thumb

Posted 2 months ago

  • 0
  • 1
This sounds like you want to use Dynamic Form Rules.  You will make a separate Dynamic Form rule for every field or every set of fields (or sections(s)) which are to behave identically.

ie you need to look at any given field and make a single rule to either show or hide that field.
Photo of AustinK


  • 1,338 Points 1k badge 2x thumb
Is there a reason you are wanting to use JavaScript for this? I was sitting here trying to visualize this and came to the same conclusion as Mark. It seems like form rules would work great and be a simpler way to accomplish this.
Photo of Raj Helaiya

Raj Helaiya

  • 1,550 Points 1k badge 2x thumb
It would be great if this could be achieved using a formula field. I am not sure how formula fields work on show hide options on other form fields. 
Photo of Raj Helaiya

Raj Helaiya

  • 1,550 Points 1k badge 2x thumb
The form rules don't work as there are hide and show constraints on same fields based on different number of activities. For example:

If number of activites =2 
show assesment method 1 and 2
hide assessment menthod 3 to 10 
show date 1 to 4
hide date 5 to 20
show pass fail 1to 4
hide pass fail 5 to 20

if number of activities = 3
show assessment method 1,2 and 3
hide assessment method 4 to 10
show date 1 to 6
hide date 7 to 20
show pass fail 1 to 6
hide pass fail 7 to 20. 

this creates show/hide conflicts on passfail and date fields. 
No problem to use formula fields.  For example make a formula field checkbox called 

[Show Field 1?] and define the formula for when to show Field 1.

Then have a Dynamic Form rule that is super easy

[Show Field 1] is checked

Show [Field 1]

Photo of Raj Helaiya

Raj Helaiya

  • 1,550 Points 1k badge 2x thumb
Can you give me an example using my scenario? 
You could use 20 formula checkbox fields.

The formula for [Show Assessment method 1]

would be

IF([# of activities]=3, true)
If there are additional considerations as to when to show [Assessment method 1], you just make the formula More complex.
Photo of Raj Helaiya

Raj Helaiya

  • 1,550 Points 1k badge 2x thumb
Hello, I need to show assesment menthod 1 field when n=#of activities is from 1 to 10 similarly show assesment method 2  when # of activities from 2 to 10. Tried using case statement , I am not able to properly define the formula
Here is a formula which will be true of the # of Activities is between 1 and 10.

[# of Activities] >=1 and [# of Activities] <=10 

Use that in the rule to control if Assessment method 1 is shown.

You will need a separate formula to control if you show Assessment Method 2. 

If the rules are as simple as this, then you do not need to have a formula though, just a form rule specifically and only dealing with Assessment Method 1.
Photo of Raj Helaiya

Raj Helaiya

  • 1,550 Points 1k badge 2x thumb
Thanks that actually worked, you are a savior