-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodo.js
More file actions
39 lines (35 loc) · 1012 Bytes
/
Todo.js
File metadata and controls
39 lines (35 loc) · 1012 Bytes
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
let button = document.getElementById('add')
let todoList = document.getElementById('todoList')
let input = document.getElementById('input');
//local storage,cookies
let todos = [];
window.onload = ()=>{
todos = JSON.parse(localStorage.getItem('todos')) || []
todos.forEach(todo=>addtodo(todo))
}
button.addEventListener('click',()=>{
todos.push(input.value)
localStorage.setItem('todos',JSON.stringify(todos))
addtodo(input.value)
input.value=''
})
function addtodo(todo){
let para = document.createElement('p');
para.innerText = todo;
todoList.appendChild(para)
para.addEventListener('click',()=>{
para.style.textDecoration = 'line-through'
remove(todo)
})
para.addEventListener('dblclick',()=>{
todoList.removeChild(para)
remove(todo)
})
}
function remove(todo){
let index = todos.indexOf(todo)
if (index > -1) {
todos.splice(index, 1);
}
localStorage.setItem('todos',JSON.stringify(todos))
}