Populate fields with array

Community Beginner ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

I have this script but it only populate first 3 field of each fields in array. What am I doing wrong?

var fields = [
["Field A","Field B","Field C","Field D","Field E","Field F"],
["Text7","Text8","Text9","Text10"],
["f23","f24","f25","f26"]];

for(var j in fields){
if(event.value == "Test"){
this.getField(fields[0][j]).value = "Test1";
this.getField(fields[1][j]).value = "Test2";
this.getField(fields[2][j]).value = "Test3";}
else{
this.getField(fields[0][j]).value = "";
this.getField(fields[1][j]).value = "";
this.getField(fields[2][j]).value = "";}}

TOPICS
JavaScript

Views

121

Likes

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

Adobe Community Professional , May 03, 2022 May 03, 2022

You can use one array instead  like this:

var fields = ["Field A","Field B","Field C","Field D","Field E","Field F","Text7","Text8","Text9","Text10","f23","f24","f25","f26"];

for(var j=0; j<=13; j++){
if(event.value == "Test"){
this.getField(fields[j]).value = "Test1";
if(j>=6 && j<=9)
this.getField(fields[j]).value = "Test2";
if(j>=10 && j<=13)
this.getField(fields[j]).value = "Test3";}
else
this.getField(fields[j]).value = "";}

Likes

Translate

Translate
Adobe Community Professional ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

for(var j in fields){

This gives the values 0, 1, and 2 for the variable j. 

Likes

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 Beginner ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

Thanks Bernd, can you help me how to access arrays to populate all fields?

Likes

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
Adobe Community Professional ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

You use 3 arrays in one array. You can access the arrays with fields [j] 

Likes

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 Beginner ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

I'm sorry but i'm not sure what you mean, i'm not very familiar with javascript and i'm doing this for couple of hours now, i tried lots of combinations but nothing worked. Can you please explain to me i would really appreciate it?

Likes

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
Adobe Community Professional ,
May 04, 2022 May 04, 2022

Copy link to clipboard

Copied

LATEST

You can access the elements like this:

for(var j in fields){
  for (var i in fields[j]) {
    ... fields[j][i] ...
  }
}

Likes

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 Beginner ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

Can someone help i still didn't solve this?

Likes

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
Adobe Community Professional ,
May 03, 2022 May 03, 2022

Copy link to clipboard

Copied

You can use one array instead  like this:

var fields = ["Field A","Field B","Field C","Field D","Field E","Field F","Text7","Text8","Text9","Text10","f23","f24","f25","f26"];

for(var j=0; j<=13; j++){
if(event.value == "Test"){
this.getField(fields[j]).value = "Test1";
if(j>=6 && j<=9)
this.getField(fields[j]).value = "Test2";
if(j>=10 && j<=13)
this.getField(fields[j]).value = "Test3";}
else
this.getField(fields[j]).value = "";}

Likes

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