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

84
Vistas
Making book.status work with localStorage

I am working on a book library app that users can add books etc, and update whether the book has been read or not by clicking a button.

The button changes style and textContent based on a button click event. It works and it updates the button ui as well as the status field of each book, but as soon as it is saved to local storage and the page reloads the button is reset and does not take into account the status field, it won't update it anymore after loading from local storage.

here is the code responsible for updating read status as well as deleting books(which works fine):

function bookEdit(event) {
  if (event.target.classList.contains("book__read-button")) {
    console.log(event.target);
    event.target.classList.toggle("clicked");
    event.target.textContent =
      event.target.textContent == "Unread" ? "Read" : "Unread";
    localStorage.setItem("library", JSON.stringify(library));
    //update object
    if (event.target.parentNode._book)
      event.target.parentNode._book.status = event.target.textContent;

    localStorage.setItem("library", JSON.stringify(library));
    console.log(library);
  }
  //-------------------Deleting Books --------------------------

  if (event.target.matches(".book__delete-button")) {
    const bookElement = event.target.parentElement;
    bookCards.removeChild(bookElement);
    const position = library.indexOf(event.target.parentNode._book);
    library.splice(position, 1);
    localStorage.setItem("library", JSON.stringify(library));

    if (bookCards.children.length === 0) {
      bookCards.innerHTML = "";
    }

    console.log(library);
  }
}

here is the JSFiddle in case you want to see the entire app.

https://jsfiddle.net/chili6/182tkLrx/14/

thanks in advance

about 4 years ago · Juan Pablo Isaza
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