I am making a website which creates some divs when a form is submitted, the problem is that these divs disappear when the form is resubmitted. Would it be possible to save these divs in sessionStorage so that when the form is resubmitted they can be appended back where they were? Something like this:
for (i = 0; i <= children.length - 1; i++) {
var selectedrow = children[i];
sessionStorage.setItem(i, selectedrow);
}
sessionStorage.setItem('total', children.length)
for (n = 0; n < sessionStorage.getItem('total'); n++) {
const rowid = sessionStorage.getItem('n');
selected.appendChild(rowid);
}
This code gives the following error Error
So is there a way I can actually do this?
Have a look at DOMParser(), this object creates a new DOM document from a string.
To use it, you instantiate a new instance. Then you use the parseFromString() method to convert your string into a new document element. The method accepts the string as it’s first argument. Set the second argument to text/html.
var stringToHTML = function (str) {
var parser = new DOMParser();
var doc = parser.parseFromString(str, 'text/html');
return doc.body;
};
stringToHTML('<your html returned from session storage>');