I wanted to get the value in the input field after clicking the search
button and set it in the localStorage
then store it in the the searchKey
variable and pass it in the main()
to search the movies in omdbapi.
I tried using the onsubmit
in the form and passed in the browse(event)
to see if I can get the value in the input field. However, when I console.log(event.target.value)
the input value is not showing up after clicking the search
button.
const movieListEl = document.querySelector('.shows');
const searchKey = localStorage.getItem("key");
async function main (event) {
const url = `https://www.omdbapi.com/?apikey=39a36280&s=${encodeURI(event)}`;
const search = await fetch(`${url}`);
const data = await search.json();
// console.log(data.Search)
movieListEl.innerHTML = data.Search.map((movie) => movieHTML(movie)).join("");
}
main(searchKey);
function browse(event){
event.preventDefault();
console.log(event.target.value);
localStorage.setItem("key", event.target.value);
}
function movieHTML (details) {
return `<div class="shows__content">
<figure class="shows__poster">
<img src="${details.Poster}" alt="" class="poster">
</figure>
<div class="poster__description">
<h4 class="poster__title">${details.Title}</h4>
<p class="poster__year">${details.Year}</p>
</div>
</div>
`
}
<form action="" id="movie__searchbox" class="nav__search-bar"
onsubmit="browse(event)">
<input type="text" id="searchbar" class="nav__search" placeholder="Search" />
<button type="submit" >
Search</button>
</form>
<div class="shows"></div>