I've been searching this website and the internet for a while now, trying to figure out how to save multiple arrays to my localStorage
. I asked a question earlier which helped me progress a bit, but I'm still struggling to grasp the concept.
I can easily work with hardcoded arrays, but integrating localStorage
data is proving to be a challenge for me.
Here's what I've attempted so far (with guidance from another SO user):
$("#submit").click(function () {
// prepare
var formData = $("#regForm").serializeArray();
// get all stored as Array []
var bookings = JSON.parse(localStorage.getItem('bookings') || '[]');
for (formData = 0; formData < localStorage.length; formData++) {
// insert and save
localStorage.setItem("bookings", JSON.stringify([formData]));
}
});
It works fine without the for loop, but whenever I submit the form again with new data, it replaces the existing information instead of creating a new index.
Essentially, I'm trying to create an appointment scheduler for dog walking where I can view, edit, delete, and display bookings without using databases on the client side.
Should I initialize my array first? How should I approach the for loop? Any help would be greatly appreciated, as I really want to learn but have been struggling with this for hours.