-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (58 loc) · 1.9 KB
/
script.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
const classNames = {
TODO_ITEM: 'todo-container',
TODO_CHECKBOX: 'todo-checkbox',
TODO_TEXT: 'todo-text',
TODO_DELETE: 'todo-delete',
}
const list = document.getElementById('todo-list')
const itemCountSpan = document.getElementById('item-count')
const uncheckedCountSpan = document.getElementById('unchecked-count')
let array = [];
let count = 0;
function newTodo() {
let cin = prompt('Enter your text:');
let todo = {id:count,text:cin,checked:false};
count+=1;
array.push(todo)
render(todo);
}
function deleteTodo(idDelete){
array = array.filter(el => el.id !== idDelete);
console.dir(array);
render({id:idDelete,tobedeleted:true})
}
function render(todo){
if (todo?.tobedeleted){
const li = document.getElementById(`todo-${todo.id}`);
li?.remove();
}
else {
const li = document.createElement('li');
li.setAttribute('id',`todo-${todo.id}`)
li.setAttribute('class',`${classNames.TODO_ITEM}`)
li.innerHTML = `<input type="checkbox" ${todo.checked ? 'checked':''} onClick=toggleCheckbox(${todo.id}) class="${classNames.TODO_CHECKBOX}"/><span class="${classNames.TODO_TEXT}">${todo.text}</span>
<button class='${classNames.TODO_DELETE}' onClick=deleteTodo(${todo.id})>DELETE</button>`
list.appendChild(li);
}
updateCount();
localStorage.setItem('array',JSON.stringify(array));
}
function updateCount(){
itemCountSpan.textContent = array.length.toString();
uncheckedCountSpan.textContent = array.filter(bon=>bon.checked===false).length.toString();
}
function toggleCheckbox(kolo){
const index = array.findIndex(el => el.id === kolo);
array[index].checked = !array[index].checked;
updateCount();
localStorage.setItem('array',JSON.stringify(array));
}
document.addEventListener('DOMContentLoaded',()=>{
const ref = localStorage.getItem('array');
if (ref){
array = JSON.parse(ref);
array.forEach(el=>{
console.log(el)
render(el)});
}
})