-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
67 lines (63 loc) · 1.41 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
let tasks=[];
const tasksList=document.getElementById("list");
const addTaskInputBox=document.getElementById("add-task");
function addTodo(task)
{
tasks.push(task);
renderList();
}
function deleteTodo(taskId)
{
const newTasks=tasks.filter(function (task){
return task.id !== taskId;
});
tasks=newTasks;
renderList();
}
function renderList()
{
tasksList.innerHTML='';
for(let i=0;i<tasks.length;i++)
{
const li=document.createElement('li');
// const task=tasks[i];
li.innerHTML=`
<input type="checkbox" id="${tasks[i].id}"/>
<label for="${tasks[i].id}"> ${tasks[i].text}</label>
<button data-taskId="${tasks[i].id}" class="delete">Delete</button>
`;
tasksList.appendChild(li);
}
}
function checkTodo(taskId)
{
const taskIndex=tasks.findIndex(function(task){
return task.id===taskId;
});
tasks[taskIndex].done=!tasks[taskIndex].done;
}
function handleClick(event)
{
if(event.target.className==='delete')
{
const taskid=Number(event.target.dataset.taskid);
deleteTodo(taskid);
}
}
function initialize()
{
document.addEventListener('click',handleClick);
document.addEventListener('keyup',function(e){
const text=e.target.value;
if(e.key==='Enter'){
const task={
text:text,
id:Date.now(),
done:false
}
addTodo(task);
}
});
renderList();
}
initialize();