-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtodolist.js
96 lines (72 loc) · 2.33 KB
/
newtodolist.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//변수선언
const main = document.querySelector(".main");
const button = document.querySelector(".button");
const list = document.querySelector(".list")
const input =document.querySelector("#input");
const form =document.querySelector("#form");
let savelist = JSON.parse( localStorage.getItem("todosave"));
let todosave = [];
//빈값 입력 방지
form.addEventListener('submit', A)
function A(a) {
a.preventDefault();
if (input.value ===""){
alert("리스트를 입력해주십시오");
} else makelist()
}
//입력 받아서 리스트 만드는 함수
function makelist(text, id, done) {
//입력값을 로컬스로지에 저장
const todoobject = {
text: input.value || savelist[i].text ,
id: Math.floor(Math.random()*1000000)+1,
done : false,
}
todosave.push(todoobject || savelist[i]);
localStorage.setItem("todosave",JSON.stringify(todosave) );
input.value = '';
const li = document.createElement("li");
const span = document.createElement("span");
const delete1 = document.createElement("button");
li.appendChild(span);
span.innerText = todoobject.text ;
list.appendChild(li);
li.appendChild(delete1);
delete1.innerText = "X"
li.id = id;
span.addEventListener("click", check(id));
if (done) {
span.classList.add("checkText");
}
//삭제버튼 누르면 삭제되는 함수
delete1.addEventListener('click', Fdelete)
function Fdelete(event) {
let li2 = event.target.parentElement;
let deletedList = li2.id
console.log(deletedList);
list.removeChild(li);
li.removeChild(span);
todosave = todosave.filter((toDo) => toDo.id !== parseInt(li2.id));
console.log(todosave);
localStorage.setItem("todosave",JSON.stringify(todosave) );
}
//클릭하면 선 생기는 함수
function check(id) {
return function (event) {
event.target.classList.toggle("checkText");
todosave = todosave.map((v) => {
if (v.id === id) {
return { ...v, done: !v.done };
}
return v;
});
localStorage.setItem("todosave", JSON.stringify(todosave));
};
}
}
//새로고침하면 데이터 유지시키는 함수
if (location.reload && localStorage.length !== 0) {
console.log("reload");
for (i=0; savelist[i]; i++){
makelist()
}}