Here is the code along with my explanation and questions:
I'm utilizing myjson.com to create 12 'results'. These results represent 12 clients, each with different sets of data. For instance,
Client 1: First Name - James,
Address - 1234 Maple Street
Client 2: First Name - Jack,
Address - 4321 Lorem Ipsum Lane
My Query: How can I populate the following 12 articles in HTML using the JSON Data with a For Loop in my JavaScript and Ajax Request?
<article>
<img class="photo" height="100" width="100">
<hgroup>
<h1 class="name"></h1>
<h2 class="email"></h2>
</hgroup>
</article>
<!-- Repeat this article structure 11 more times -->
`const Employees = (function () {
let displayStudent = document.querySelector('.photo');
let name = document.querySelector('.name');
let email = document.querySelector('.email');
let phone = document.querySelector('.phone');
let streetAddress = document.querySelector('.streetAddress');
let city = document.querySelector('.city');
let state = document.querySelector('.state');
let zip = document.querySelector('.zip');
const ajaxCall = function () {
let hr = new XMLHttpRequest();
let url = 'https://api.myjson.com/bins/zfhmr';
hr.onreadystatechange = function () {
if (hr.readyState === 4) {
if (hr.status === 200) {
let myObj = JSON.parse(hr.responseText);
for(let i = 0; i < myObj.length; i++) {
displayStudent.src = myObj.results[i].picture.large;
name.innerHTML = myObj.results[i].name.first + " " + myObj.results[i].name.last;
}
}
} else {
console.log("ajax error: " + hr.response);
}
};
hr.open("GET", url, true);
hr.send();
};
return {
init: function () {
ajaxCall();
}
};
}());
Employees.init();`
I'm struggling to populate more than one article at a time with just one client's information. Any assistance would be highly appreciated!
Thank you