Skip to main content
Known Participant
January 4, 2021
해결됨

Checkbox auto check if

  • January 4, 2021
  • 2 답변들
  • 821 조회

Hello there and happy new year 2021 !

I am actually working on a form and I am struggling with some JS. 

 

I have two sets of checkoxes (let's say CB1 and CB2 to sum up) with 4 instances for each with different export values (CB1 : A, B, C, D // CB2 : A, B, C, D). The first set corresponds to the classification of a client and the second one corresponds to the possibility for the client to overwrite the above mentionned information with a new one (A client which is CB1 A can decide to be CB2 C). So far, no trouble for me. 

 

Here comes my problem : 

Later on the document, the value of the selected checkbox is repeated as a sum up of the document (with checkboxes) and I have to find a way to express the following :

 

IF CB2 is not checked, take the value of CB1
IF, CB2 is checked, take the value of CB2. 

 

Can someone help me, I am still learning JS... ?

 

Thanks. 

 

Guillaume

 

 

 

 

이 주제는 답변이 닫혔습니다.
최고의 답변: try67

Ah, it's a check-box... In that case use the code as the custom calculation script of a (hidden) text field, and adjust the last line to be:

this.getField("CB3").value = (cb2!="Off") ? cb2 : cb1;

2 답변

try67
Community Expert
Community Expert
January 4, 2021

You can use this code as the custom calculation script of your field:

 

var cb1 = this.getField("CB1").valueAsString;

var cb2 = this.getField("CB2").valueAsString;

event.value = (cb2!="Off") ? cb2 : cb1;

Known Participant
January 4, 2021

Hi, 

Thanks a lot for you help. onsidering the "field " CB3 is a set of 4 checkboxes, I am not sure this will work. 

So far, I was thinking about this script to be added as a mouse up event of CB1 and CB2

if (this.getField("CB2").value != "Off") {this.getField("CB3").value = this.getField("CB2").value;} else {this.getField("CB3").value = this.getField("CB1").value;}

 

It seems to work, but I am not sure this is the best way. 

try67
Community Expert
try67Community Expert답변
Community Expert
January 4, 2021

Ah, it's a check-box... In that case use the code as the custom calculation script of a (hidden) text field, and adjust the last line to be:

this.getField("CB3").value = (cb2!="Off") ? cb2 : cb1;

Inspiring
January 4, 2021

A script fragment might be somehting like:

 

// Set the CB_val variable to the value of CB1, or CB2 if no CB1 items have been selected

var CB_val = getField("CB1").value !== "Off" ? getField("CB1").value : getField("CB2").value;

 

You can then use CB_val as needed. Note that this probably doesn't correctly handle the case where both CB1 and CB2 are "Off" (none in either group are selected). If you need help modifying the script to account for this, post again with more information.