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);
}