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

174
Vistas
how use localstorage in todo app (javascript)

How can I use the storage property in the following code? This code does not work properly and disappears after each refresh.

Please check my code in the link below https://jsfiddle.net/74qxgonh/

let values = [];

// TAG Form ( give text)
document.querySelector("form").addEventListener("submit", (e) => {
  e.preventDefault();

  // create object  (unique id)
  storageValue = {
    id: "num" + Math.floor(Math.random() * (1000, 9000)),
    text: e.target.elements.textInput.value,
  };

  //create Array for save to localStorage
  values.push({ storageValue });
  localStorage.setItem("values", JSON.stringify(values));
  
  //create Tags
  let input = document.createElement("input");
  input.setAttribute("type", "checkbox");
  input.setAttribute("class", "chk-box");
  input.setAttribute("id", `${storageValue.id}`);

  let label = document.createElement("label");
  label.textContent = `${storageValue.text}`;
  label.setAttribute("for", `${storageValue.id}`);
  label.setAttribute("class", "chk-label");

  const btnDeleted = document.createElement("button");
  btnDeleted.setAttribute("class", "btn-delete");
  btnDeleted.textContent = "X";
  btnDeleted.addEventListener("click", () => {
    div.remove();
  });

  const div = document.createElement("div");
  div.setAttribute("class", "inputGroup");
  div.appendChild(input);
  div.appendChild(label);
  div.appendChild(btnDeleted);

  document.querySelector(".container-center").appendChild(div);

  e.target.elements.textInput.value = "";
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You probably just need to replace the first line of your script to actually get the previously saved items from localStorage instead of starting with an empty array every time the app loads:

// before
let values = [];
// after
let values;

try {
  values = JSON.parse(localStorage.getItem('values')) ?? [];
}
catch {
  values = [];
}
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