My current script allows you to add a card + some info about its content by clicking on the result of a search bar.
The objective is to create a sort of movies list. However, I do not know how, where or if I should implement a loop to add multiple cards, instead of replacing one.
My biggest issue is that after clicking on the result item, the active card is replaced by the new one.
The first function loads the content based on an API. It basically clears the search bar after clicking on the item shown through the research. It also calls the function responsible for adding a div (a card with the movie informations) to the HTML.
function loadContentDetails(){
const searchListContent = searchList.querySelectorAll('.search-list-item');
searchListContent.forEach(content => {
content.addEventListener('click', async () =>{
searchList.classList.add('hide-search-list');
searchBar.value = "";
const result = await fetch(`http://www.omdbapi.com/?i=${content.dataset.id}&apikey=60ee3e91`);
const contentDetails = await result.json();
displayContentDetails(contentDetails);
});
});}
The second function creates a new card based on what the user selected through the search bar.
function displayContentDetails(details){
cardsGroupFlex.innerHTML = `
<div class="card">
<img src="${(details.Poster != "N/A") ? details.Poster: "resources/img-not-found.png"}" alt="" class="card-img" />
<div class="card-description">
<p class="card-title">${details.Title}</p>
<p> ${(details.Plot)} </p>
</div>
</div>
`;
}
I would much appreciate your help.