I created a simple website to add and delete items in to do list where the user is able to input items and add to the current list. I want users to be able to save current list and when they open from same browser, they can see the previously edited list from last time. In other word: using Local Storage function to achieve this (not using any server side scripting or database). Not sure how I can achieve this by user hitting save button. Here is my html code:
<html>
<body>
<div id="myDIV" class="header">
<h1>My To Do List</h1>
<input type="text" id="myInput" placeholder="Add to the list">
<span onclick="addToList()" class="addBtn">Add</span>
</div>
<ul id="myUL">
<li>Go to grocery</li>
<li>Pay bills</li>
<li>Go to class</li>
<li>Clean home</li>
<li>Do laundry</li>
<li>Do homework</li>
</ul>
<button onclick="saveList()" type="button">Save</button>
<script src="index.js"></script>
</body>
Here is my JavaScript code:
function addToList() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var tn = document.createTextNode(inputValue);
li.appendChild(tn);
if (inputValue === '') {
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}
The saveList() function is what I want to define to allow users to save the list so that they can close the browser and reopen with saved data retrieved from local storage. Hope that makes sense.