Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
Local storage with api, vanilla js

To make this short and sweet my code is

const searchBox = document.querySelector(".search-box");
searchBox.addEventListener("keypress", setQuery);

function setQuery(e) {
  if (e.keyCode == 13) {
    getResults(searchBox.value);
    searchBox.value = "";
  }
}

function getResults(query) {
  localStorage.setItem("location", searchBox.value);
  let storedItem = localStorage.getItem("location");
  console.log(storedItem);
  fetch(`${api.base}weather?q=${storedItem}&units=imperial&APPID=${api.key}`)
    .then((weather) => {
      return weather.json();
    })
    .then(displayResults);
}

This all works great and the city that is typed into the search box is stored into the local storage just fine and if page is refreshed I can look in the google chrome app section and see it still in local storage.

After the page refresh tho the app resets to the default landing page.

I'm thinking I need to use something like window.onload = function()

the problem is if I'm setting the storedItem I can't access outside of the getResults function.

any suggestions would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  • Remove localStorage.setItem("location", searchBox.value); from getResults() and add it to setQuery().

  • Next, add this to getResults()

    let qry = query == undefined ? 
    localStorage.getItem("location") : query;
    

    This means if there isn't any query passed to getResults(query) then get the value from localStorage under the key "location".

  • Finally bind the DOMContentLoaded event to document and getResults() as event handler,

    document.addEventListener('DOMContentLoaded', getResults)  
    

const searchBox = document.querySelector(".search-box");
searchBox.addEventListener("keypress", setQuery);

function setQuery(e) {
  if (e.keyCode == 13) {
    getResults(searchBox.value);
    localStorage.setItem("location", searchBox.value);
    searchBox.value = "";
  }
}

function getResults(query) {
  let qry = query == undefined ? localStorage.getItem("location") : query;
  console.log(qry);
  fetch(`${api.base}weather?q=${qry}&units=imperial&APPID=${api.key}`)
    .then((weather) => {
      return weather.json();
    })
    .then(displayResults);
};

document.addEventListener('DOMContentLoaded', getResults)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda