-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (47 loc) · 1.49 KB
/
script.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
let todolistdata = []
const listTodo = document.getElementById("todo-list")
const addButton = document.getElementById("add")
addButton.addEventListener("click", function (){
const newText = document.getElementById("new-todo")
if (newText.value === ""){return}
const newItem = {
description : newText.value,
done : false
}
todolistdata.push (newItem)
console.log (todolistdata)
renderTodo ()
setLocal()
newText.value = ""
})
function renderTodo (){
listTodo.innerHTML = ""
todolistdata.forEach((item,index) => {
const li = document.createElement ("li")
li.innerHTML = item.description
listTodo.appendChild(li)
const checkbox = document.createElement ("input")
checkbox.type = "checkbox"
checkbox.checked = item.done
li.appendChild(checkbox)
checkbox.addEventListener("change", ()=>{
todolistdata[index].done = !todolistdata[index].done
renderTodo()
} )
})
}
const deletedButton = document.getElementById("deleted")
deletedButton.addEventListener("click",function(){
todolistdata = todolistdata.filter (item=> item.done == false)
renderTodo ()
setLocal ()
})
const setLocal = () => {
localStorage.setItem("todos", JSON.stringify(todolistdata))
}
const getLocal = () => {
const data = localStorage.getItem("todos")
todolistdata=JSON.parse(data)
}
getLocal()
renderTodo()