So I'm sending a request to NASA's APOD API and receiving back an array of info, part of which is an image url. I need to perform this action multiple times, and am wondering how to set this up as a for loop, or maybe just a function(?). The HTML is like this:
<img id="apod1">
<img id="apod2">
<img id="apod3">
...etc
And the JavaScript is like this:
// APOD1 XML REQUEST
var apod1Url = "https://api.nasa.gov/planetary/apod?concept_tags=True";
var apod1Xml = new XMLHttpRequest();
apod1Xml.open('GET', apod1Url, true);
apod1Xml.send(null);
// APOD1 URL FETCH AND IMAGE RENDER
apod1Xml.onreadystatechange=function() {
if (apod1Xml.readyState==4 && apod1Xml.status==200) {
var apod1Parse = JSON.parse(apod1Xml.responseText);
document.getElementById('apod1').src = apod1Parse.url;
console.log(apod1Parse.url);
}
}
I know you can set up a for loop like:
for (i) { do stuff }
But how would I feed these imgs into something like that? Hopefully this is clear enough.