I'm a full stack dev student, I have an event listener, that includes a fetch call in Javascript, but the gifs being returned are each in their own response container. Each time I click the button, a new image container is created. How do I fix it so that the gifs only appear in the same container? I also want to display a random flow of photos when my buttons are clicked, not just one. Any help is appreciated.
Here's my code for the first two buttons.
window.addEventListener('fetch', function (event) {
console.log("fetch add event listener");
});
natureBtn.addEventListener('click', function (event) {
fetch(
'https://api.giphy.com/v1/gifs/random?api_key=MY_API_KEY&rating=g&tag=nature'
)
// Convert the response to JSON
.then(function (response) {
return response.json();
})
.then(function (response) {
// Use 'querySelector' to get the ID of where the GIF will be displayed
var responseContainerEl = document.querySelector('#response-container');
// Create an '<img>' element
var gifImg = document.createElement('img');
// Set that element's 'src' attribute to the 'image_url' from our Giphy API response
gifImg.setAttribute('src', response.data.image_url);
// Append the '<img>' element to the page
responseContainerEl.appendChild(gifImg);
});
});
sportsBtn.addEventListener('click', function (event) {
fetch(
'https://api.giphy.com/v1/gifs/random?api_key=MY_API_KEY&rating=g&tag=sports'
)
// Convert the response to JSON
.then(function (response) {
return response.json();
})
.then(function (response) {
// Use 'querySelector' to get the ID of where the GIF will be displayed
var responseContainerEl = document.querySelector('#response-container');
// Create an '<img>' element
var gifImg = document.createElement('img');
// Set that element's 'src' attribute to the 'image_url' from our Giphy API response
gifImg.setAttribute('src', response.data.image_url);
// Append the '<img>' element to the page
responseContainerEl.appendChild(gifImg);
});
});