javascripthtmlasync-awaitlocal-storageomdbapi

target input value in the form to store in the localStorage after submitting the form and use the value for omdbapi


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>


Solution

  • Why do you get the value of the search button through event.target? The event object corresponds to the form submission as far as I remember so it's better to get it directly.

    Also, inline attributes like onsubmit() are a bad practice, use the addEventListener() method instead.

    And, call the main() function, update local storage, and get <input>'s value when the form is submitted.

    And, here's your final code.

    I commented everything related to local storage because it doesn't work in Stack Snippets.

    const movieListEl = document.querySelector(".shows");
    const moviesForm = document.getElementById("movie__searchbox");
    
    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();
        movieListEl.innerHTML = data.Search.map((movie) => movieHTML(movie)).join("");
    }
    
    // check when the form is submitted
    moviesForm.addEventListener("submit", (event) => {
        const searchBarValue = document.getElementById("searchbar").value; // get input's value everytime the form is submitted
        console.log(searchBarValue);
        event.preventDefault(); // prevent refresh
        // localStorage.setItem("key", searchBarValue);
        // const searchKey = localStorage.getItem("key");
        main(searchBarValue);
    });
    
    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">
        <input type="text" id="searchbar" class="nav__search" placeholder="Search" />
        <button type="submit">Search</button>
    </form>
    <div class="shows"></div>

    See this JSFiddle for a working version using local storage.