I am trying to get data from each movieCard into an object and add it to an array to use in local storage to make a watchlist. When I try to add a for loop to go through the buttons I only get the data from the last button. I'm not sure how to grab the data for each card. I am still pretty new to javascript so any help would be appreciated.
//api key
const apikey = ''
//grab DOM elements
const searchInput = document.getElementById('input-el')
const searchBtn = document.getElementById('button-el')
const movieList = document.getElementById('movies')
//array of objects that makes up watchlist
let watchlist = []
//determine whether to display add or remove button
let addRemoveWatchlist = ''
//fetch api data if search input is truthy
const fetchMovies = async(searchTerm) => {
const URL = `http://www.omdbapi.com/?apikey=${apikey}&s=${searchTerm}`
const res = await fetch(`${URL}`)
const movies = await res.json()
if(movies.Response == 'True') {
displayMovieList(movies.Search)
} else {
movieList.innerHTML = `
<section class='movie-card'>
We can't seem to find anything . . . try being more specific
</section>
`
}
}
const fetchMoreInfo = async(movieId) => {
const URL = `http://www.omdbapi.com/?apikey=${apikey}&i=${movieId}`
const res = await fetch(`${URL}`)
const moreInfo = await res.json()
// console.log(moreInfo)
return moreInfo
}
//loop through the api data array and display in html
const displayMovieList = async(movies) => {
movieList.innerHTML = ''
let movieCard = document.createElement('div')
movies.forEach(async (movie) => {
let data = await fetchMoreInfo(movie.imdbID)
movieCard =
`<section class='movie-card'>
<img src='${data.Poster}' class='movie-img' />
<div class='movie-info'>
<section class='section1'>
<div class='movie-title'>${data.Title}</div>
<div class='movie-year'>${data.Year}</div>
<div class='imdb-rating'><i class="fa-solid fa-star"></i>${data.imdbRating}</div>
</section>
<section class='section2'>
<div class='movie-rating'><b>${data.Rated}</b></div>
<div class='movie-runtime'>${data.Runtime}</div>
<button class='watchlist'>Add to Watchlist</button>
</section>
<section class='section3'>
<div class='actors'><b>Actors:</b> ${data.Actors}</div>
<div class='director'><b>Director:</b> ${data.Director}</div>
<div class='plot'>${data.Plot}</div>
</section>
</div>
</section>
<hr>`
movieList.innerHTML += movieCard
})
const watchlistEl = document.querySelectorAll('.watchlist')
for (let i = 0; i < watchlistEl.length; i++) {
watchlistEl[i].addEventListener('click', () => {
watchlist.push(data.Title)
console.log(watchlist)
})
}
}
//grab search bar input
const findMovies = () => {
let searchTerm = searchInput.value
fetchMovies(searchTerm)
}
searchBtn.addEventListener('click', findMovies)
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
searchBtn.click()
}
})