Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

83
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda