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

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

658

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 , 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 = "";}

Votes

Translate

Translate
Community Expert ,
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. 

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 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?

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 ,
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] 

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 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?

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 ,
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] ...
  }
}

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

Copy link to clipboard

Copied

Can someone help i still didn't solve this?

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 ,
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 = "";}

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