Skip to main content
Known Participant
November 28, 2024
Answered

Concatenate text

  • November 28, 2024
  • 2 replies
  • 932 views

I have five text fields (Text1 to Text5) and five corresponding checkboxes (Check Box1 to Check Box5). I need to create a script such that when a checkbox is selected, the value of the corresponding text field is displayed in a separate field. If multiple checkboxes are selected, the values of the associated text fields should be concatenated and displayed, separated by commas. Could you advise on how to implement this functionality using a custom script?

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

Is it possible to add text by order it is checked?


Try this:

if (typeof orderArray === "undefined") {
 var orderArray = [];}

var txt = [];

for (var i=1; i<=5; i++) {
 var checkbox = this.getField("Check Box"+i).valueAsString;
 var text = this.getField("Text"+i).valueAsString;

 if (checkbox !== "Off") {
  if (orderArray.indexOf(i) === -1) {
   orderArray.push(i);}} 
 else {
  var index = orderArray.indexOf(i);
  if (index !== -1) {
   orderArray.splice(index, 1);}}}

for (var idx of orderArray) {
 txt.push(this.getField("Text" + idx).valueAsString);}

event.value = txt.length ? txt.join("\n") : "";

2 replies

MarietaaAuthor
Known Participant
November 30, 2024

I changed field to multiline, how to show each value in separate row?

Nesa Nurani
Community Expert
Community Expert
November 30, 2024

Change:

join(",") with join("\n")

MarietaaAuthor
Known Participant
December 6, 2024

Thanks.


Is it possible to add text by order it is checked?

Nesa Nurani
Community Expert
Community Expert
November 28, 2024

Use this as custom calculation script in a field where you want to show text:

var txt = [];
for(var i=1; i<=5; i++){
 var c = this.getField("Check Box"+i).valueAsString;
 var t = this.getField("Text"+i).valueAsString;
 
 if(c !== "Off")
  txt.push(t);}

event.value = txt.length ? txt.join(",") : "";