I am making a simple API call to load a random picture on screen when a button is clicked. I would like the image to refresh and show a new random image each time I click the button. Currently it just keeps adding new pictures below the current one. How can I get it to refresh with a new random picture onclick without stacking them? Thanks
loadImage = (url) => {
fetch(url)
.then(response => response.json())
.then(newResponse => displayData(newResponse,'dataDiv'))
}
function displayData(apiResponse, divid){
let gridDiv = document.getElementById("gridDiv");
const imageDiv = document.createElement("img");
imageDiv.id = "Pic";
imageDiv.src = apiResponse[0]['url'];
gridDiv.appendChild(imageDiv);
}
<div class="container py-3">
<button type="button" class=" btn btn-success" id="Button" onclick="loadImage('https://apicall###')"
Load Random Pictures!
</button>
</div>
<div class="container">
<div class="data-grid-container" id="gridDiv"></div>
</div>