What Am I trying to achieve: -less DOM calls in viewData function and overall a cleaner aproach to it.
My code and what I've done so far:
function viewData(){
var uid=document.getElementById("viewUserId").value;
for(i in users){
if(users[i].id==uid){
document.getElementById("nameEditInput").value=users[i].name;
document.getElementById("userNameEditInput").value=users[i].username;
document.getElementById("emailEditInput").value=users[i].email;
document.getElementById("streetEditInput").value=users[i].address.street;
document.getElementById("suiteEditInput").value=users[i].address.suite;
document.getElementById("cityEditInput").value=users[i].address.city;
document.getElementById("zipEditInput").value=users[i].address.zipcode;
document.getElementById("latEditInput").value=users[i].address.geo.lat;
document.getElementById("lngEditInput").value=users[i].address.geo.lng;
}
}
My idea :
I thought of giving my inputs a common class instead of an ID (for example "viewInfo") and create an array that stores my inputs. After that,I could parse through that array and assign my object values to my class input array.The problem I encountered with this was that I didn't know how to parse through my object.
var x = document.getElementsByClassName('viewInfo');
for(i in users){
if(users[i].id==uid){
for(k in x){
x[k].value=users[k].[i]; //this gives me an error :Unexpected token [
}}}