I'm messing around with the Harry Potter API (just to learn how API's work), and have successfully gotten the images from the site onto mine. But I have created like 8 functions to do it and I feel like it could be done a lot easier. Here is the code:
JS:
// API
url = "http://hp-api.herokuapp.com/api/characters";
fetch(url)
.then(function(response){
return response.json();
})
.then(function(data){
display_image(data[1].image);
display_image2(data[2].image);
display_image3(data[3].image);
display_image4(data[4].image);
display_image5(data[5].image);
display_image6(data[6].image);
display_image7(data[7].image);
display_image8(data[8].image);
})
.catch(function(error){
console.log("Error: " + error)
});
function display_image(image_url){
document.getElementById("image1").src = image_url;
}
function display_image2(image_url){
document.getElementById("image2").src = image_url;
}
function display_image3(image_url){
document.getElementById("image3").src = image_url;
}
function display_image4(image_url){
document.getElementById("image4").src = image_url;
}
function display_image5(image_url){
document.getElementById("image5").src = image_url;
}
function display_image6(image_url){
document.getElementById("image6").src = image_url;
}
function display_image7(image_url){
document.getElementById("image7").src = image_url;
}
function display_image8(image_url){
document.getElementById("image8").src = image_url;
}
HTML:
<div class="popularContainer">
<div class="grid-1">
<img width="100%" alt="" id="image1">
</div>
<div class="grid-2">
<img width="100%" alt="" id="image2">
</div>
<div class="grid-3">
<img width="100%" alt="" id="image3">
</div>
<div class="grid-4">
<img width="100%" alt="" id="image4">
</div>
<div class="grid-5">
<img width="100%" alt="" id="image5">
</div>
<div class="grid-6">
<img width="100%" alt="" id="image6">
</div>
<div class="grid-6">
<img width="100%" alt="" id="image7">
</div>
<div class="grid-6">
<img width="100%" alt="" id="image8">
</div>
</div>
Also, more of a "if I do this" thing than a practical use right now. But let's say I put a random index on the data and got a random picture everytime the user reloaded the page. Is there a way to distinguish if there are duplicates and have one or more of them rerun so it's not a duplicate anymore? Also could I make it to where if one was a certain character it would not show up (with .style.display maybe)?
Anyways, any help is appreciated.