Copy link to clipboard
Copied
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;
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;
Copy link to clipboard
Copied
You should put the names of the fields in an array, and then iterate over it in your for-loop.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
You need to better explain how this should work. Are there two sets of fields, or just one?
Copy link to clipboard
Copied
there are 3 or more fields converted into one. But to each line i need unique start. for example
field1 contains text 10.000
field2 contains text 100.000
field 3 contains text 1.000
field 4 need to contain all three field but with unique text as line identification in this case it should like this
loan: 10.000
mortgage: 100.000
credit card: 1.000
loan, mortgage and credit card are not fields but variables depending on the number of the line. In this case its variable i
a1="loan";
a2="mortgage";
a3="credit card";
var text = "";
for (var i=1; i<=3; i++) {
var s = this.getField("AMOUNT"+i).valueAsString;
if (s!="") text+=""+ ?? + ": " + s + "\r"; //if i=1 then use a1, if i=2 use a2, if i=3 use a3
}
event.value = text;
but bernd Alheit code works, its just more complicated then i thought. I thought i can use i variable in for loop to identify variable a1-a3
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.