Skip to main content
Inspiring
September 23, 2020
Answered

Conditionnal check box

  • September 23, 2020
  • 2 replies
  • 4313 views

I'm new to JS. I know it should be easy to do that but I didn't find my answer anywhere.

I have a check box that I want to be checked only if there is a value in either one of my three drop-down list. 

Every script I tried didn't work out. 

How should I write it ?

 

If Drop1 or Drop 2 or Drop 3 is not empty, check Box 1

Thanks again, 

This topic has been closed for replies.
Correct answer Nesa Nurani

Hi, sorry guys for meddling, if I got it right you want to check checkbox if values are not empty in one of 3 dropdown fields
and to be unchecked if no items are selected in dropdowns?
If thats all you want, add empty value to each dropdown and give it export value of 0,
then you can use this code as custom calculation script of one of dropdown fields or another hidden text field:

if(this.getField("Decoupe").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblagedessus").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblagepattes").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else this.getField("Dessin").checkThisBox(0, false);

2 replies

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
September 23, 2020

Hi, sorry guys for meddling, if I got it right you want to check checkbox if values are not empty in one of 3 dropdown fields
and to be unchecked if no items are selected in dropdowns?
If thats all you want, add empty value to each dropdown and give it export value of 0,
then you can use this code as custom calculation script of one of dropdown fields or another hidden text field:

if(this.getField("Decoupe").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblagedessus").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblagepattes").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else this.getField("Dessin").checkThisBox(0, false);

Inspiring
September 24, 2020

Thanks ! It works and this method is simple for me as I am new to JS.

Karl Heinz  Kremer
Community Expert
Community Expert
September 23, 2020

I assume that the checkbox should never be checked or unchecked by the user directly, so make it read-only. 

 

Now the problem is that a checkbox does not have a calculation script associoated with it, so the best way to do that (at least in my opinion), is to have another field in your form that is hidden and read-only. This way, it will not show up on the form, and the user cannot accidentially tab into it. I use a text field for that, and I usually name it "hiddenCalculation" to indicate what it's purpose is. When you then use the following script (after modifying the field names to match your form) as the calculation script in the hidden field, it should work:

 

var d1 = this.getField("Dropdown1").value;
var d2 = this.getField("Dropdown2").value;
var d3 = this.getField("Dropdown3").value;

var cb = this.getField("Checkbox");

cb.value = (d1 || d2 || d3) ? "Yes" : "Off";
Inspiring
September 23, 2020

Thanks for this. 

The issue is that the check box could be check by the user too...

I have a form that will be filled by the customer to order some products. 

There is options (the three drop downs) that can have a value or not.

If the client chose one of the option, he must send a drawing with his form. That's why the box should be checked if something is selected in one of the drop downs.

But, he can choose to send a drawing even if none of theese options are checked. 

If you have another solution for me I can do it differently. 

 

Inspiring
September 23, 2020

Don't make it read only. Also how did you make so dropdown doesn't have value?