skip to Main Content

I am trying to save in-session changes to an html table to localStorage so I can have the changes stay even after reload.

I’ve not tried anything yet as I heard localStorage can only save strings and I need to save a table

2

Answers


  1. You can save it as a string and convert it back to an array.

    const array = [1, 2, 3];
    localStorage.setItem('myArray', JSON.Stringify(array));
    
    const myArray = JSON.Parse(localStorage.getItem('myArray'));
    
    console.log(myArray) // array: [1, 2, 3]
    
    Login or Signup to reply.
  2. I think that you have 2 options:

    1. Save the table html: localStorage.setItem('table', table.innerHTML) and then, you can replace the table with that content table.innerHTML = localStorage.getItem('table'))
    2. Serialize the data to string and save it: localStorage.setItem('table', JSON.stringify(data)) and then data = JSON.parse(localStorage.getItem('table')) and process it.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search