How do I repopulate a list with local storage json data?

  Kiến thức lập trình

This is my first time using this resource so bare with me. I’m working on a choose your own adventure game. I need to be able to take a ul, store it in local storage, and repopulate a ul on another page using the stored ul. I have been trying to use json to do this as I have been given the impression that it is required but I am having trouble repopulating the ul on a new page.

JS
// itemList is the id for the UL I want to copy, it’s size varies. This is how I create/store it

     let item = document.createElement('li');
     let a = document.createElement("a");
  
     a.textContent = "New Item";
     a.setAttribute('href', "javascript:functionName()");
     
     let itemList = document.getElementById('inventory');
     item.appendChild(a);

     itemList.id = 'test';
     itemList.appendChild(item);

     localStorage.setItem("test", JSON.stringify(itemList));
  

// from what I have found online this line should work I think for retrieving and repopulating 
    let textList = JSON.parse(localStorage.getItem("test"));
    document.getElementById("testHTML").innerHTML = textList;

All I end up getting in my repopulated ul is “undefined”. What am I missing?

New contributor

dpjollygreen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT