I am creating a website where the user can add items to a To Do list. It is possible to add items and also delete them, i did that using JavaScript. But now I want to save the current items of the list when the page is refreshed.
I want the To Do list to save the current items, including the ones added by the user, so that when the page is reloaded the list doesn't go back to its initial form but still maintains the items added. I don't know how to do it, any help would be appreciated as I'm currently learning HTML, CSS and JavaScript.
// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
myNodelist[i].appendChild(span);
}
// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
}
}, false);
// Create a new list item when clicking on the "Add" button
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
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";
}
}
}
#myUL {
position: absolute;
width: 30rem;
margin-left: 72rem;
margin-top: 21rem;
padding: 0;
list-style-type: none;
}
#myUL li {
font-family: 'Montserrat', sans-serif;
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
background: #eee;
font-size: 18px;
transition: 0.2s;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#myUL li:nth-child(odd) {
background: #f9f9f9;
}
#myUL li:hover {
background: #ddd;
}
#myUL li.checked {
background: #888;
color: #fff;
text-decoration: line-through;
}
#myUL li.checked::before {
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
}
.close:hover {
background-color: #f44336;
color: white;
}
.to-do-header {
position: absolute;
width: 30rem;
margin-top: 13.5rem;
margin-left: 72rem;
padding: 1rem 1rem 1rem 1rem;
background-color: #9fa77f;
color: white;
text-align: center;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
}
.to-do-header:after {
content: "";
display: table;
clear: both;
}
.table-title {
margin-bottom: 0.6rem;
font-family: 'Quicksand', sans-serif;
}
#myInput {
font-family: 'Montserrat', sans-serif;
padding: 0.6rem;
margin: 0;
border: none;
border-radius: 0;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
.addBtn {
font-family: 'Quicksand', sans-serif;
padding: 0.6rem;
width: 25%;
background: #d9d9d9;
color: #555;
float: left;
text-align: center;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
border-radius: 0;
}
.addBtn:hover {
background-color: #bbb;
}
<div id="myDIV" class="to-do-header">
<h2 class="table-title">To Do</h2>
<input type="text" id="myInput" placeholder="What do you want to do?">
<span onclick="newElement()" class="addBtn">Add</span>
</div>
<ul id="myUL">
<li>Wake up</li>
<li>Meeting with the team</li>
<li>Have an amazing lunch</li>
<li>Finsish the project</li>
<li>Get some pizza</li>
<li>Go to sleep</li>
</ul>