I wrote the following code, which works finely:
document.getElementById('firstName').value = persons[checkboxId].firstName;
document.getElementById('lastName').value = persons[checkboxId].lastName;
document.getElementById('street').value = persons[checkboxId].street;
document.getElementById('zipCode').value = persons[checkboxId].zipCode;
document.getElementById('city').value = persons[checkboxId].city;
document.getElementById('mobile').value = persons[checkboxId].mobile;
document.getElementById('email').value = persons[checkboxId].email;
Now I want to write this code more clearly using a loop. So I tried this:
let inputFields = ['firstName', 'lastName', 'street', 'zipCode', 'city', 'mobile', 'email'];
for (let i = 0; i < inputFields.length; i++) {
document.getElementById(inputFields[i]).value = persons[checkboxId].inputFields[i];
}
But it doesn't work. The problem is with // persons[checkboxId].inputFields[i] // I get an "Cannot read properties" - error.
Persons is a JSON like this:
let persons = [
{
firstName : 'ABC',
lastName : 'DEF'
},
{
firstName : 'CHG',
lastName : 'KJI'
}
]
Is there a way to read the properties using a loop?
Thank you very much for help!