I'm building a scheduling web app, and it shows a table of rows with time of day, a text area, and a save button side by side for each hour of the day. The user can type their info into the textarea next to a given time, click the button, and it will save their input to localStorage. On refresh, the localStorage still shows the saved user input when I console.log it, but it won't repopulate the textareas with it. I want their user input to still show up in those textareas when they refresh the page, but something about how I'm using localStorage.getItem doesn't seem to be working. I'd love some help, and thank you!
(Sorry in advance that the images are posting as links, it won't let me embed them)
Here's an example of the HTML I have for each row of the table, which includes the textarea.
This is my current JS for adding the user input to localStorage.
And this is all I've got for my getItem function so far, which is what I need help fixing!
--
UPDATE: This is the final code that fixed my issues!
// save button was clicked
for (var i = 0; i < saveBtns.length; i++){
saveBtns[i].addEventListener("click", function(event) {
event.preventDefault();
// define event text array
var events = [];
for (var i = 9; i <= 17; i++) {
events[i] = document.getElementById(i).value;
}
//save events to localStorage
localStorage.setItem("events", JSON.stringify(events));
console.log(localStorage);
})};
// load saved user input on refresh
var loadEvents = function() {
var events = JSON.parse(localStorage.getItem("events"));
// populate the textareas
for (var hour in events) {
if (events[hour]) // check if value is not null
document.getElementById(hour).value = events[hour];
}
}
loadEvents();