• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Show certain values in a textfield based on checkbox input

New Here ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

Hello,

 

Javascript is not really my cup of tea, so I'm hoping someone here is able to help me out with this problem.. In the form I'm creating, it is needed that a certain value is shown in a certain textfield based on which checkbox is marked. There are 4 checkboxes (CheckBoxWarranty, CheckBoxWarranty1, CheckBoxWarranty2, CheckBoxWarranty3). This is set up in a way that the user only can check 1 checkbox (it is not possible to check multipe checkboxes).  But depending on which checkbox is marked, a certain value need to be shown in the textfield Result. Below you'll find a overview of which value that needs to be shown for each checkbox.

 

CheckBoxWarranty = 10

CheckBoxWarranty1 = 12

CheckBoxWarranty2 = 15

CheckBoxWarranty3 = 20

 

Based on other discussions, I was already able to make this work for 1 checkbox with the following code as a custom calculation script for the textfield.

event.value = (this.getField("CheckBoxWarranty").valueAsString=="Off")? "" : "10"; 

 

But I don't really know how to go further from here. I already tried adding the same lines for the other checkboxes to the code. But tthis result in always showing the results for the first line, I assume the first line simply overwrites the others. I assume this needs some conditional-statements? So some help here would really be appreciated.

 

Thanks in advance.

 

 

 

TOPICS
How to , JavaScript

Views

461

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

You could do it with export value, but it would need to be set correctly,
but it's easier to put values in a script, if you know all the values for each checkbox,
you can put them in an array and then use the loop to populate fields (years)
with correct value.
Since you didn't provide field names or values let's say you put export value for checkboxes to be 1 for 1st checkbox, 2 for 2nd, 3 for 3rd and 4 for 4th, and let's say you have 20 fields named "Year1" to "Year20"
you can use this script in any text field as custom calculation script, only input your values in arrays 'check1' to 'check4':

var check1 = [10, 20, 30, 40];
var check2 = [5, 15, 23];
var check3 = [1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var check4 = [9, 12, 18, 122];

for (var i=0; i<20; i++) {
var c = this.getField("CheckBoxWarranty").valueAsString;
var year = this.getField("Year" + (i + 1));
    
if (c == "1" && i < check1.length) {
 year.value = check1[i];} 
else if (c == "2" && i < check2.length) {
 year.value = check2[i];} 
else if (c == "3" && i < check3.length) {
 year.value = check3[i];} 
else if (c == "4" && i < check4.length) {
 year.value = check4[i];} 
else {
 year.value = "";}}

 

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

If you want user to select only one checkbox you need to make checkboxes mutually exclusive by giving all your checkboxes same name (name all 4 checkboxes "CheckBoxWarranty") and set export values of each checkbox to be the number you want, for example give 1st checkbox export value 10, 2nd checkbox, export value 12...etc

In text field, as custom calculation script use this:

var c = this.getField("CheckBoxWarranty").valueAsString;
event.value = (c == "Off") ? "" : c; 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

Thank you so much for your answer. I had already given my checkboxes the same name. This indeed works great if the output is the same everytime. Sorry, I should have mentioned this in my original post but if the checkbox is checked, it needs to automatically fill in mutiple fields in a table I created. In the screenshot you can see the result for example if the first checkbox  is checked. The second checkbox will generate orther values for these fields and also values for the 11th and 12th year. The third will also generate totaly different values but will also include values for the 13th, 14th and 15th year. ..  using the export value probably won't be work here?

 

liability.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

You could do it with export value, but it would need to be set correctly,
but it's easier to put values in a script, if you know all the values for each checkbox,
you can put them in an array and then use the loop to populate fields (years)
with correct value.
Since you didn't provide field names or values let's say you put export value for checkboxes to be 1 for 1st checkbox, 2 for 2nd, 3 for 3rd and 4 for 4th, and let's say you have 20 fields named "Year1" to "Year20"
you can use this script in any text field as custom calculation script, only input your values in arrays 'check1' to 'check4':

var check1 = [10, 20, 30, 40];
var check2 = [5, 15, 23];
var check3 = [1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var check4 = [9, 12, 18, 122];

for (var i=0; i<20; i++) {
var c = this.getField("CheckBoxWarranty").valueAsString;
var year = this.getField("Year" + (i + 1));
    
if (c == "1" && i < check1.length) {
 year.value = check1[i];} 
else if (c == "2" && i < check2.length) {
 year.value = check2[i];} 
else if (c == "3" && i < check3.length) {
 year.value = check3[i];} 
else if (c == "4" && i < check4.length) {
 year.value = check4[i];} 
else {
 year.value = "";}}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 31, 2024 May 31, 2024

Copy link to clipboard

Copied

LATEST

I just tried this and it works perfectly! Thank you so much for your help 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines