The original script did not provide the means to load the logs from localStorage
, and stored new input under the "logs" key, while the intent was to prepend. The solution is quite simple: load from localStorage on page load, and overwrite localStorage['logs']
with the contents of logContents
as displayed on the page.
....
window.onload = function(){
existing_log = localStorage.getItem("logs");
console.log(existing_log);
if (existing_log){document.getElementById("logContent").innerHTML = existing_log;}
}
function enterPressed() {
if (textBox.value == "") {
return;
}
else {
var logText = today + ":" + " " + document.getElementById("textBox").value + '<br>\n';
$("#logContent").prepend(logText); // Prepend textBox's content to the top of "logContent".
textBox.value = ""; // To clear the textBox after enter is pressed.
localStorage.setItem("logs", document.getElementById("logContent").innerHTML);
}
}