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

combining two variables

New Here ,
Jan 09, 2023 Jan 09, 2023

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;

TOPICS
Acrobat SDK and JavaScript

Views

347

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

correct answers 1 Correct answer

Community Expert , Jan 10, 2023 Jan 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;

Votes

Translate

Translate
Community Expert ,
Jan 10, 2023 Jan 10, 2023

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.

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 ,
Jan 10, 2023 Jan 10, 2023

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;

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 ,
Jan 10, 2023 Jan 10, 2023

Copy link to clipboard

Copied

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

 

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 ,
Jan 10, 2023 Jan 10, 2023

Copy link to clipboard

Copied

LATEST

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

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 ,
Jan 10, 2023 Jan 10, 2023

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;

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 ,
Jan 10, 2023 Jan 10, 2023

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.

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