skip to Main Content

I got a table on my site which I want to save in the session storage and replace it on button click (discard changes). Seems to work well to save it in session storage, but now I dont know how to make it usable for my HTML, that I can replace the current table with the table in my storage.

Tried to convert it with this , but I only got this: enter image description here

I hoped that I can store it like this: [table output2 and just set it later on.

This is my current code.

    $(document).ready(function () {
        let table = document.querySelector('.overallTable');  
        sessionStorage.setItem('init', JSON.stringify(table));

        document.getElementById("dcChanges").addEventListener("click", function () {                                            
            if (sessionStorage.getItem('init') == sessionStorage.getItem('cache')) {console.log("equal")}
        });

        document.getElementById("saveInCache").addEventListener("click", function () {
            document.getElementsByClassName('overallTable');
            sessionStorage.setItem('cache', table);
            console.log(table);
            console.log("Output: Table: " + sessionStorage.getItem('cache'));
            console.log("Output: Converted: " + JSON.stringify(sessionStorage.getItem('cache')));
        });
      });

I hoped I just could do something like this document.getElementsByClassName("overallTable").innerHTML = sessionStorage.getItem('cache');

2

Answers


  1. try document.getElementsByClassName('overallTable')[0].innerHTML

    Login or Signup to reply.
  2. You can use html() function of jQuery.

    $('.overallTable').html();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search