-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
71 lines (63 loc) · 1.81 KB
/
todo.js
File metadata and controls
71 lines (63 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const toDoForm = document.querySelector(".js-toDoForm");
const toDoInput = toDoForm.querySelector("input");
const toDoList = document.querySelector(".js-toDoList");
const TODOS_LS = "toDos";
let toDos = [];
let listIndex = 1;
init();
function init() {
loadStorage();
toDoForm.addEventListener("submit", toDoSubmit);
}
function loadStorage() {
const localStorageToDos = localStorage.getItem(TODOS_LS);
if(localStorageToDos != null) {
const parsedToDos = JSON.parse(localStorageToDos);
for(var i=0; i<parsedToDos.length; i++) {
addList(parsedToDos[i].text);
}
}
}
function toDoSubmit(e) {
e.preventDefault();
const inputValue = toDoInput.value;
toDoInput.value = "";
addList(inputValue);
}
//<i class="fa fa-trash"></i>
function addList(listValue) {
const li = document.createElement("li");
const delBtn = document.createElement("button");
const tagI = document.createElement("i");
tagI.classList.add("fa");
tagI.classList.add("fa-trash");
tagI.classList.add("fa-2x");
const span = document.createElement("span");
const liId = listIndex++;
delBtn.appendChild(tagI);
tagI.addEventListener("click", deleteList);
span.innerHTML = listValue;
li.appendChild(delBtn);
li.appendChild(span);
li.id = liId;
toDoList.appendChild(li);
const toDo = {
text: listValue,
id: liId
};
toDos.push(toDo);
saveLocalStorage();
}
function saveLocalStorage() {
localStorage.setItem(TODOS_LS, JSON.stringify(toDos));
}
function deleteList(e) {
const li = e.target.parentNode;
const lili = e.target.parentNode.parentNode;
const cleanToDos = toDos.filter(function(toDo) {
return toDo.id != lili.id;
});
toDoList.removeChild(lili);
toDos = cleanToDos;
saveLocalStorage();
}