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

pichimnc

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>

Sally loves Lightning

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

store the localstorage value in the form of json

React Router redirect after submitting a form using the input value

adding an input value and submitting the form on load

Change input text value in before submitting form

Symfony 3: set value after submitting a form

Django Form Field Not Setting Value After Submitting

Get the check box value after submitting a form

Insert LocalStorage value into Form Input Field with Javascript

Unable to hold form 1 value after submitting form 2

noUiSlider -> use value as form input

localStorage page does not display after submitting form

Not submitting default value in GET form

Submitting form using selection value with form data

how to instantly load a list of options after submitting a value in a django form

Keep $_GET value of a drop down menu after submitting the form

How to compare value of the form before and after submitting in Symfony?

Session loses its data or value after form submitting in php

Not able to get form element's value after submitting form using this.form.submit();

How can I use javascript to add a value to a form before submitting it?

Take input value from submit form and store in redux store to use later

Unable to retrieve select input value form window.localstorage

React - clearing an input value after form submit

Keep input value after form submit (with a catch)

Angular retaining input value after form reset

Prevent Wrong Input in JS After Submitting A Form

Set the focus to input after submitting the form

rails use form input value before save

How to calculate two input form fields and put the value in another without submitting the form using jquery with JSF

Get value form input