-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
88 lines (76 loc) · 2.63 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
// const input = document.querySelector('#inputer');
// const list = document.querySelector('#lister');
// const btn = document.querySelector('#btnx');
// const parag = document.querySelector('#para');
// btn.addEventListener('click', (f) => {
// f.preventDefault()
// if(input.value === '') {
// parag.innerHTML = 'Put In Something'
// setTimeout(() => {parag.remove()}, 2000)
// }
// else{
// const li = document.createElement('li')
// li.classList.add('list-group-item')
// li.appendChild(document.createTextNode(`${input.value}`))
// list.appendChild(li)
// }
// } )
const inputBoxx = document.querySelector('#inputer');
const addBtn = document.querySelector('#btnx');
const toDoList = document.querySelector('#lister');
const deleteAllBtn = document.querySelector('#resp .footer');
inputBoxx.onkeyup = () => {
let userData = inputBoxx.value
if(userData.trim() != '') {
addBtn.classList.add('active');
}
else{
addBtn.classList.remove('active');
}
}
displayTasks();
addBtn.onclick = () => {
let userData = inputBoxx.value;
let getLocalStorage = localStorage.getItem('New Todo');
if(getLocalStorage == null) {
listArr = [];
} else{
listArr = JSON.parse(getLocalStorage);
}
listArr.push(userData)
localStorage.setItem('New Todo', JSON.stringify(listArr))
displayTasks();
}
function displayTasks() {
let getLocalStorage = localStorage.getItem('New Todo');
if(getLocalStorage == null) {
listArr = [];
} else{
listArr = JSON.parse(getLocalStorage);
}
const pendingNum = document.querySelector('.pendingNum');
pendingNum.textContent = listArr.length;
if(listArr.length > 0) {
deleteAllBtn.classList.add('active')
} else {
deleteAllBtn.classList.remove('active')
}
let newLiTag = '';
listArr.forEach( (value, index, array) => {
newLiTag = newLiTag `<li class="list-group-item" id="buy">${value}<span class="spam" onclick="removeTasks(${index})"><button class="btn btn-danger btn-sm">-</button></span></li><br>`;
} );
toDoList.innerHTML = newLiTag;
inputBoxx.value = '';
}
function removeTasks(index) {
let getLocalStorage = localStorage.getItem('New Todo');
listArr = JSON.parse(getLocalStorage);
listArr.splice(index, 1)
localStorage.setItem('New Todo', JSON.stringify(listArr))
displayTasks();
}
deleteAllBtn.onclick = () => {
listArr = [];
localStorage.setItem('New Todo', JSON.stringify(listArr))
displayTasks();
}