-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.js
41 lines (36 loc) · 1.05 KB
/
home.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
let button = document.getElementById('add');
let todoList = document.getElementById('todoList');
let input = document.getElementById('input');
let list = document.getElementById("todoList");
//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('li');
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))
}