My goal with block 2 is to streamline my existing block 1 code by defining javascript variables in a for loop.The code below is giving me the error "Unexpected token [" in the console. I am not sure I am using the proper syntax here. Is there a way to loop over an array and insert each dynamically to get the desired result of block 1?
I am able to console.log details_object[i] in block 2 and successfully see all the array elements but that is about as far as I can get.
/*Block 1*/
/*var title = document.getElementById('title').value;
var phone = document.getElementById('phone').value;
var firstn = document.getElementById('firstn').value;
var lastn = document.getElementById('lastn').value;
var displayn = document.getElementById('displayn').value;*/
/*Block 2 less code!*/
var details_object = ["title", "phone", "firstn", "lastn", "displayn"];
for(var i=0, l = details_object.length; i < l; i++){
var details_object[i] = document.getElementById("'"+details_object[i]+"'").value;
}
"'"parts in the function call, each of the array elements are already strings.