Skip to main content
Participant
January 10, 2023
Answered

combining two variables

  • January 10, 2023
  • 2 replies
  • 1059 views

Hi,

i have this code that revrites multiple textfields into one. i need to change variable i into a1, a2 or a3 depending ond the value of i.

//final text should look like this

//text10: loan1

//text20: loan2

//text30: loan3

CODE:

a1=("text10");
a2=("text20");
a3=("text30");


var text = "";
for (var i=1; i<=3; i++) {
var s = this.getField("loans"+i).valueAsString;
if (s!="") text+=""+ i +  ": " + s + "\r";
}
event.value = text;

This topic has been closed for replies.
Correct answer Bernd Alheit

Try this:

a1=("text10");
a2=("text20");
a3=("text30");

var text = "";
for (var i=1; i<=3; i++) {
  var s = this.getField("loans"+i).valueAsString;
  if (s!="") {
    var txt;
    if (i == 1) txt = a1;
    else if (i == 2) txt = a2;
    else txt = a3;
    text+=""+ txt +  ": " + s + "\r";
  }
}
event.value = text;

2 replies

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
January 10, 2023

Try this:

a1=("text10");
a2=("text20");
a3=("text30");

var text = "";
for (var i=1; i<=3; i++) {
  var s = this.getField("loans"+i).valueAsString;
  if (s!="") {
    var txt;
    if (i == 1) txt = a1;
    else if (i == 2) txt = a2;
    else txt = a3;
    text+=""+ txt +  ": " + s + "\r";
  }
}
event.value = text;
Participant
January 10, 2023

thanks this one works like a charm.  if variable i will be for example from 1 to 20 the code will build up.. but it should work as well, thanks.

try67
Community Expert
Community Expert
January 10, 2023

You should put the names of the fields in an array, and then iterate over it in your for-loop.

Participant
January 10, 2023

do you mean like this? its not working, but i have never worked with arrays before 😞


var fields = ["jedna","dva","tři"];

var text = "";
for (var i=1; i<=3; i++) {
var s = this.getField("PUJCKY"+i).valueAsString;
if (s!="") text+=""+ fields[i].value + ": " + s + "\r";
}
event.value = text;

try67
Community Expert
Community Expert
January 10, 2023

You need to better explain how this should work. Are there two sets of fields, or just one?