-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (66 loc) · 1.77 KB
/
Copy pathscript.js
File metadata and controls
73 lines (66 loc) · 1.77 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
72
73
let input=document.querySelector("input");
let addbtn=document.querySelector("#Add_btn");
let list=document.querySelector("#to-do-list");
const saved=localStorage.getItem('todos');
const todos=saved? JSON.parse(saved):[];
function savetodos(){
localStorage.setItem('todos',JSON.stringify(todos));
}
function createTodoList(todo,idx){
const li=document.createElement("li");
const checkbox=document.createElement("input");
checkbox.type="checkbox"
checkbox.checked=!!todo.completed;
checkbox.addEventListener("change",()=>{
todo.completed=checkbox.checked;
savetodos();
render();
})
// text of the todos
const textspan=document.createElement("span");
textspan.textContent=todo.text;
textspan.style.marginBottom="7px";
if(todo.completed){
textspan.style.textDecoration = "line-through";
}
textspan.addEventListener("dblclick",()=>{
const newtext=prompt("Edit Text",todo.text);
if(newtext!=null){
todo.text=newtext;
textspan.textContent=todo.text;
savetodos();
}
})
// Delete button
const Delete=document.createElement("button");
Delete.textContent="Delete";
Delete.addEventListener("click",()=>{
todos.splice(idx,1);
render();
savetodos();
})
// append to list
li.appendChild(checkbox);
li.appendChild(textspan);
li.appendChild(Delete);
return li;
}
function render(){
list.innerHTML='';
todos.forEach((todo,idx) => {
const node=createTodoList(todo,idx);
list.appendChild(node);
});
}
function addtoDo(){
const text=input.value.trim();
if(!text){
return;
}
todos.push({text,completed:false});
input.value='';
render();
savetodos();
}
addbtn.addEventListener("click",addtoDo);
render();