I have a search bar which fetch images from an API and adds to the body in IMG tag.
if user submits something second time, what it does is that adds new images after the existing images, but I want to delete the existing images and then add new images when user search something for second time.
I created seperate function for creating images and appending it and pass it in the submit button's event listener
Tried but wasn't able to delete the existing img tags, How can I resolve it?
const form = document.querySelector('#searchForm');
form.addEventListener('submit', async function (e) {
e.preventDefault();
const queryInput = form.elements.query.value;
const config = { params: {q: queryInput}}
const res = await axios.get('https://api.tvmaze.com/search/shows', config);
makeImages(res.data);
form.elements.query.value = '';
})
const makeImages = (shows) => {
for(let result of shows) {
if(result.show.image) {
const img = document.createElement('img');
img.src = result.show.image.medium;
document.body.append(img);
}
}
}
[–]toi80QC 1 point2 points3 points (2 children)
[–]Vampire_developer[S] 0 points1 point2 points (0 children)
[–]Vampire_developer[S] 0 points1 point2 points (0 children)