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:
I hoped that I can store it like this: [ 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
try
document.getElementsByClassName('overallTable')[0].innerHTML
You can use
html()
function of jQuery.$('.overallTable').html();