-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
103 lines (95 loc) · 2.68 KB
/
main.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
window.onload = () => {
'use strict';
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('sw.js');
}
}
const inbox = document.getElementById('inbox');
const addbtn= document.getElementById('addbtn');
const clrbtn= document.getElementById('clrbtn');
const todo = document.querySelector('.todolist');
let indexed = 0;
showtask();
inbox.onkeyup = ()=>{
let userData = inbox.value;
if(userData.trim() != 0){
addbtn.classList.add("active");
}
else{
addbtn.classList.remove("active");
}
}
addbtn.onclick = ()=>{
let userData = inbox.value;
let getlocal = localStorage.getItem("New Todo");
if(getlocal == null){
listarr = [];
}else{
listarr = JSON.parse(getlocal);
}
listarr.push(userData);
localStorage.setItem('New Todo', JSON.stringify(listarr));
showtask();
}
function showtask(){
let getlocal = localStorage.getItem("New Todo");
if(getlocal == null){
listarr = [];
}else{
listarr = JSON.parse(getlocal);
}
let newlitag = '';
listarr.forEach((item, i) => {
//newlitag += '<li>' + item + '<span onclick="openit('+ i +')">+<span onclick="deleteit('+ i +')" >-</span></span></li>';
newlitag += '<li>' + item + '<div class="span" style="display:inline" onclick="openit('+ i +')">+</div><div class="spans" style="display:inline" onclick="deleteit('+i+')">-</div></li>';
todo.innerHTML = newlitag;
inbox.value = "";
addbtn.classList.remove("active");
let number = document.getElementById('num');
number.innerHTML = listarr.length;
});
}
function save(){
alert("saving it...");
let getlocal = localStorage.getItem("New Todo");
let edit = document.getElementById('exampleModalLongTitle');
listarr = JSON.parse(getlocal);
listarr[indexed] = edit.innerHTML;
$('#exampleModalCenter').modal('hide');
localStorage.setItem('New Todo', JSON.stringify(listarr));
showtask();
}
function openit(index){
let getlocal = localStorage.getItem("New Todo");
listarr = JSON.parse(getlocal);
$('#exampleModalCenter').modal('show');
let edit = document.getElementById('exampleModalLongTitle');
edit.innerHTML = listarr[index];
indexed = index;
}
function deleteit(index){
if (confirm("You want to delete it!")) {
txt = 1;
} else {
txt = 2;
}
if(txt == 2)
{
return;
}
let getlocal = localStorage.getItem("New Todo");
listarr = JSON.parse(getlocal);
listarr.splice(index, 1);
localStorage.setItem('New Todo', JSON.stringify(listarr));
if(listarr.length == 0){
location.reload();
}
showtask();
}
clrbtn.onclick = ()=>{
listarr = [];
localStorage.setItem('New Todo', JSON.stringify(listarr));
location.reload();
showtask();
}