1

When I click submit, the table is created as I want it to, and the data is saved into the local storage. When I refresh, the table is gone, but the data remains in local storage. I'm assuming that I have to load the data from local storage into the table on refresh, but I have explored a good amount of possible fixes but I can't seem to get it to work... Any help would be much appreciated.

const TitleInput = document.getElementById("Title");
const DateInput = document.getElementById("Date");
const ThoughtsInput = document.getElementById("Thoughts");
const displayTable = document.getElementById("mainTable");
const img = document.getElementById("happy");

document.getElementById("button").addEventListener("click", function(){
    document.querySelector(".popup").style.display = "flex";
})

document.querySelector(".close").addEventListener("click", function(){
    document.querySelector(".popup").style.display = "none";
})


document.getElementById("popupSubmit").addEventListener("click", function(event){
    const Title = TitleInput.value;
    const Date = DateInput.value;
    const Thoughts = ThoughtsInput.value;
    if(!localStorage.getItem("Title")){
    localStorage.setItem("Title", Title);
    }
    if(!localStorage.getItem("Date")){
    localStorage.setItem("Date", Date);
    }
    if(!localStorage.getItem("Thoughts")){
    localStorage.setItem("Thoughts", Thoughts);
    }

    const data = {
        Title: Title,
        Date: Date,
        Thoughts: Thoughts
    }
    
    
    event.preventDefault();


    template(data);
    
    document.querySelector(".popup").style.display = "none";
});

function template(data){
    displayTable.insertAdjacentHTML("beforeend", `
    <tr>
        <th>Title: ${data.Title}</th>
        <th>Date: ${data.Date}</th> 
        <td id="userInput">My Thoughts: <br> ${data.Thoughts}</td>
        <td><img src = "../public/img/HappyFace.png"/></td>
    </tr>
    `)


}

Here is the onload function that I added thanks to Barmar

window.onload=function(){
    const TitleInStorage = localStorage.getItem("Title");
    const DateInStorage = localStorage.getItem("Date");
    const ThoughtsInStorage = localStorage.getItem("Thoughts");
    const data = {
        Title: TitleInStorage,
        Date: DateInStorage,
        Thoughts: ThoughtsInStorage
    }
    template(data);
}
Bick
  • 11
  • 2
  • Use a `window.onload` function that reads the data from Local Storage and recreates the table. – Barmar Dec 14 '21 at 21:35
  • Hey @Barmar, thanks for the reply, hopefully, you will see this... I have taken your advice, and it works, but only for the first entry I make. So when I refresh I can see the table created, but when I add a new one and refresh, it only shows the first one, I will edit my post with the new window.onload function. – Bick Dec 14 '21 at 21:48
  • You're only saving one thought to local storage. You should put all the table data in an array of structures, convert that to JSON, and save that. – Barmar Dec 14 '21 at 21:50
  • @Barmar, okay, I'm not gonna lie I'm really bad at this and trying to learn lol. I'm not expecting you to explain it to me, but do you know what I should look up so I can learn how to put the data in 'an array of structures' and convert it to JSON? Thank you so much for the help. Would I do that in the onload function? – Bick Dec 14 '21 at 21:53
  • I'm not sure how to explain it any better without just writing it for you. – Barmar Dec 14 '21 at 21:56
  • Understood, thank you for the help! – Bick Dec 14 '21 at 21:57
  • @Barmar Do you know if the array needs to be created inside of my popupSubmit.eventlistener? – Bick Dec 14 '21 at 22:14
  • Yes. Whenever you save, you need to save the entire array. – Barmar Dec 14 '21 at 22:14
  • @Barmar, should I change the localStorage.setItem stuff to the array? Or keep everything I have and just add it to an array, and then save it so it can be retrieved? – Bick Dec 14 '21 at 22:17
  • See these examples: https://stackoverflow.com/questions/61824345/how-to-add-a-new-object-to-an-array-of-objects-in-localstorage https://stackoverflow.com/questions/13702100/localstorage-array-of-objects-handling – Barmar Dec 14 '21 at 23:41

0 Answers0