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

373
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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