-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
70 lines (59 loc) · 1.5 KB
/
todo.js
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
const toDoForm = document.querySelector('.js-toDo');
const toDoInput = toDoForm.querySelector('input');
const toDoList = document.querySelector('.js-toDoList');
const TODO_LS = 'toDos';
let toDo = [];
delToDo = (event) =>{
const btn = event.target;
const li = btn.parentNode;
toDoList.removeChild(li);
const cleanToDo = toDo.filter(function(toDo){
return toDo.id !== parseInt(li.id);
});
toDo = cleanToDo;
saveToDo();
}
saveToDo = ()=>{
localStorage.setItem(TODO_LS, JSON.stringify(toDo))
}
paintToDo = (a) => {
const li = document.createElement('li');
const span = document.createElement('span');
const delBtn = document.createElement('div');
delBtn.addEventListener('click', delToDo);
const newId = toDo.length + 1;
span.innerHTML = a;
delBtn.innerText = '❤';
delBtn.classList.add('delBtn')
li.appendChild(delBtn);
li.appendChild(span);
li.id = newId;
toDoList.appendChild(li);
const toDoObj = {
text: a,
id: newId
}
toDo.push(toDoObj);
saveToDo();
}
handleSubmit = (event) => {
event.preventDefault();
const currentValue = toDoInput.value;
paintToDo(currentValue);
// after input, delete input value in the input box
toDoInput.value = '';
}
loadToDo = () => {
const loadedToDo = localStorage.getItem(TODO_LS);
if (loadedToDo !== null){
const parsed = JSON.parse(loadedToDo);
parsed.forEach(function(toDo) {
paintToDo(toDo.text)
});
}
}
init = () => {
loadToDo();
toDoForm.addEventListener('submit', handleSubmit)
}
init()