-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
106 lines (95 loc) · 3.08 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const addbutton=document.querySelector('.add');
const todoul=document.querySelector('.todolist');
const inpu=document.querySelector('.inpu');
const filter=document.querySelector('.filt');
const alli=document.getElementsByTagName('li')
addbutton.addEventListener("click",todo);
document.addEventListener('DOMContentLoaded',gettodo)
function todo(event){
event.preventDefault();
const tododiv = document.createElement('div');
const todoli =document.createElement('li');
const trash=document.createElement('button');
const complete=document.createElement('button');
complete.classList.add('com');
tododiv.classList.add('di')
trash.classList.add('styl');
trash.innerHTML='-';
complete.innerHTML='+';
let ip=inpu.value
savetodo(ip);
todoli.innerHTML=inpu.value;
todoli.classList.add('list');
tododiv.appendChild(todoli);
tododiv.appendChild(complete);
tododiv.appendChild(trash);
todoul.appendChild(tododiv);
inpu.value=null;
trash.addEventListener('click',deletelist);
complete.addEventListener('click',comp);
}
function deletelist(e){
removetodo(e.target.parentElement);
e.target.parentElement.classList.add('fall');
addEventListener('transitionend',function(){
e.target.parentElement.remove()
}
);
}
function comp(e){
e.target.parentElement.classList.toggle('compl')
}
filter.addEventListener('click',filtr);
function filtr(){
for(let i=0;i<alli.length;i++){
if(alli[i].parentElement.classList.contains('compl')){
alli[i].parentElement.remove();
i=i-1;
}
}
}
function savetodo(todo){
let todos;
if(localStorage.getItem('todos')===null){
todos= [];
}
else{
todos=JSON.parse(localStorage.getItem('todos'));
}
todos.push(todo);
localStorage.setItem("todos",JSON.stringify(todos));
}
function gettodo(){
let todos;
if(localStorage.getItem('todos')!=null){
todos=JSON.parse(localStorage.getItem('todos'));
todos.forEach(function(todo){
const tododiv = document.createElement('div');
const todoli =document.createElement('li');
const trash=document.createElement('button');
const complete=document.createElement('button');
complete.classList.add('com');
tododiv.classList.add('di')
trash.classList.add('styl');
trash.innerHTML='-';
complete.innerHTML='+';
todoli.innerHTML=todo;
todoli.classList.add('list');
tododiv.appendChild(todoli);
tododiv.appendChild(complete);
tododiv.appendChild(trash);
todoul.appendChild(tododiv);
trash.addEventListener('click',deletelist);
complete.addEventListener('click',comp);
});
}
}
function removetodo(todo){
let todos;
if(localStorage.getItem('todos')!==null){
todos=JSON.parse(localStorage.getItem('todos'));
todos.splice(todos.indexOf(todo.children[0].innerText),1);
localStorage.setItem("todos",JSON.stringify(todos));
}
console.log(todo.children[0].innerText)
}