Skip to content

Commit f28678f

Browse files
committed
[day 6] Store list items with LocalStorage
1 parent 9a9289d commit f28678f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

projects/6-to-do-list/js/to-do.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ var form = document.getElementById('todo-form');
22
var toDoList = document.getElementById('todo-items');
33
var newToDo = document.getElementById('todo-add-new');
44

5-
var toDoArray = [];
6-
75
var addToDo = function() {
86
var txt = newToDo.value;
9-
toDoArray.push(txt);
10-
11-
var items = '<li class="todo__item">' + toDoArray.join('</li><li class="todo__item">') + '</li>';
12-
toDoList.innerHTML = items;
7+
var item = document.createElement("li");
8+
item.innerHTML = txt;
9+
item.classList.add("todo__item");
10+
toDoList.appendChild(item);
1311
newToDo.value = '';
1412

13+
var contents = toDoList.innerHTML;
14+
localStorage.setItem('contents', contents);
15+
1516
var toDos = document.querySelectorAll('li.todo__item');
1617

1718
var completeToDo = function() {
@@ -21,6 +22,8 @@ var addToDo = function() {
2122
for(var i = 0; i < toDos.length; i++) {
2223
toDos[i].addEventListener('click', completeToDo, false);
2324
}
25+
26+
return false;
2427
};
2528

2629

@@ -50,3 +53,7 @@ newToDo.onkeydown = function(event) {
5053
return false;
5154
}
5255
};
56+
57+
if(localStorage.getItem('contents')) {
58+
toDoList.innerHTML = localStorage.getItem('contents');
59+
}

0 commit comments

Comments
 (0)