Skip to main content
Inspiring
September 23, 2020
Answered

Conditionnal check box

  • September 23, 2020
  • 2 replies
  • 4319 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

I tried it. So I created an hidden field. Marked it as read only.

I also marked the check box as read only.

I copied the code and remplaced the values. It doesn't work. What can be wrong ? I put the script in the personnalized calculation section of the hidden field.

 

var d1 = this.getField("Decoupe").value;
var d2 = this.getField("Assemblagedessus").value;
var d3 = this.getField("Assemblagepattes").value;

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

cb.value = (d1 || d2 || d3) ? "Yes" : "Off";

Inspiring
September 23, 2020

The last three lines show me that your first dropdown uses one space as the "blank" element, you've very likely added that to the list of options. The other two show a true empty string, which is what my script expects. The error messages indicate that there is a problem with one or more of your field names. Would you be able to share the form you are working on? If you are not able to share this publicly, you can also share it with me directly. My contact information should be in my profile. 


Yes I could share my file, but I don't see any options to attach a file even in the private message section.