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

370
Views
LocalStorage and update the data on each input JS

I am trying to save the data into my object from input fileds using the localStorage but when I add a value into input and save it, it saves but when I try to add new value it deletes the old value and adds the new one instead. My demands are in the following :

  • how to make it not replace the old entry and just take the new one?
  • if I do render the data to html it shows me just what it is in the links obj it doesn't show me
  • every entry should update the links obj with new entry in localStorage. how can I do that ?

here is the code : JavaScript:

let links = {
    team1: {
      icon: "FCL.png",
      list: [{ name: "Real Madrid", url: "www.Real Madrid.com" }]
    },
    team2: {
      icon: "FCL.png",
      list: [{ name: "FC Barcelona", url: "www.FCB.com" }]
    },
    fav: {
      icon: "fav.png",
      list: []
    }
  };
  $("#click").click(function () {
    const link = document.querySelector("#link").value;
    const name = document.querySelector("#name").value;
    links["fav"].list.push({ url: link, name: name });
    localStorage.setItem("links", JSON.stringify(links));
    console.log(localStorage.setItem("links", JSON.stringify(links)));
  });

HTML:

<form name="form1" action="">
      <input type="text" name="link" id="link" />
      <input type="text" name="name" id="name" />
      <button id="click">click</button>
    </form>

here sandbox : enter link description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

So, the problem is when you are reloading web page, the links variable is getting the initial value you declared in your code. If you want to keep persisting the updating links, you need to first check if links already exists in localStorage, if so, then assign it to linksor else set the default value.

Example:

const localStorageLinks = JSON.parse(localStorage.getItem("links"))

let links = localStorageLinks || {
    team1: {
      icon: "FCL.png",
      list: [{ name: "Real Madrid", url: "www.Real Madrid.com" }]
    },
    team2: {
      icon: "FCL.png",
      list: [{ name: "FC barcelona", url: "www.FCL.com" }]
    },
    fav: {
      icon: "fav.png",
      list: []
    }
  };

Now, even if you reload the page your links will have the latest updated links.

about 4 years ago · Juan Pablo Isaza Report

0

setItem() method will add the key to the given LocalStorage object, or update (replace) that key's value if it already exists. If
You have to use JSON.parse() method parses a JSON string in LocalStorage, add the new value.

list = JSON.parse(localStorage.getItem("links") || [];
list.push({ url: link, name: name });
localStorage.setItem("links", JSON.stringify(links));
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!