0

How I can display localStorage information on my webpage?

I am easily setItem() to localStorage and when I console.log() it is showing but I cannot display it on the page(after reloading it is gone) I wanna keep this data on my page even when I am closing the tab Thank you in advance

const title = document.querySelector("#title");
const author = document.querySelector("#author");
const rating = document.querySelector("#rating");
const category = document.querySelector("#category");
const bookList = document.querySelector("#book-list");

document.querySelector("#book-form").addEventListener("submit", (e) => {
  e.preventDefault();
});
document.querySelector("#submit-btn").addEventListener("click", function () {
  if (
    title.value === "" ||
    author.value === "" ||
    rating.value === "" ||
    category.value === ""
  ) {
    alert("Please fill the form");
  } else {
    //  Creating tr th and appending to list
    const bookListRow = document.createElement("tr");
    const newTitle = document.createElement("th");
    newTitle.innerHTML = title.value;
    bookListRow.appendChild(newTitle);
    const newAuthor = document.createElement("th");
    newAuthor.innerHTML = author.value;
    bookListRow.appendChild(newAuthor);
    const newRating = document.createElement("th");
    newRating.innerHTML = rating.value;
    bookListRow.appendChild(newRating);
    const newCategory = document.createElement("th");
    newCategory.innerHTML = category.value;
    bookListRow.appendChild(newCategory);

    const deleteBtn = document.createElement("th");
    deleteBtn.classList.add("delete");
    deleteBtn.innerHTML = "X";
    bookListRow.appendChild(deleteBtn);

    bookList.appendChild(bookListRow);

    //Storage
    let storageTitle = title.value;
    let storageAuthor = author.value;
    let storageRating = rating.value;
    let storageCategory = category.value;

    localStorage.setItem("title", JSON.stringify(storageTitle));
    localStorage.setItem("author", JSON.stringify(storageAuthor));
    localStorage.setItem("rating", JSON.stringify(storageRating));
    localStorage.setItem("category", JSON.stringify(storageCategory));

    for (var i = 0; i < localStorage.length; i++) {
      newTitle += localStorage.getItem(localStorage.key(i));
    }
    // Clear
    title.value = "";
    author.value = "";
    rating.value = "";
    category.value = "";
  }
});

// Remove each books by clicking X button
bookList.addEventListener("click", (e) => {
  e.target.parentElement.remove();
}); ```

3 Answers3

0

the question of your code may is that you just save the data to the localStoarge, but at the initial of the page ,you did't get the data from the localStorage, you should get data like this:

window.onload = function (){
  let storageTitle = JSON.parse(localStorage.getItem("title"));
  document.querySelector("#title").innerHtml = storageTitle;
}
screw spike
  • 371
  • 1
  • 6
0

this code should be working can you specify the problem a little further!

0

You are setting it correctly but not reading it as you should. Local storage persists data even if you close the tab, so it is just your code that is causing you trouble. You can find an explanation how you should work with local and session storage here. https://stackoverflow.com/a/65655155/2563841

Sasa
  • 32
  • 3