Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!