-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (88 loc) · 2.81 KB
/
app.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
107
108
109
110
111
'use strict'
const controls = document.querySelector('.controls')
const todo_title = document.querySelector('#todo-title')
const todoList = document.getElementById('todo_render')
const duedate = document.getElementById('duedate')
const new_todo = document.querySelector('.new_todo')
const line = document.querySelector('hr')
let todos;
const savedTodos = JSON.parse(localStorage.getItem('todos'))
if (Array.isArray(savedTodos)) {
todos = savedTodos
} else {
todos = [{
title: 'Build todo list',
due: '2024-02-20',
id: 'id0'
}]
}
render()
function delTodo(event) {
const delbtn = event.target
const idToRemove = delbtn.id
todos = todos.filter(function (todos) {
if (todos.id === idToRemove) {
return false
} else {
return true
}
})
saveTodoList()
render()
}
function render() {
todoList.innerHTML = ''
todos.forEach(function (todos) {
const todo = document.createElement('div')
const todo_list = document.createElement('p')
const todo_list_date = document.createElement('p')
const delbtn = document.createElement('button')
const chkbx = document.createElement('input')
// ischeck = chkbx.value
chkbx.type = 'checkbox'
chkbx.style = 'margin-left:10px;height: 44px;width: 20px;'
chkbx.className = 'chkbx'
delbtn.innerText = 'Remove'
delbtn.id = todos.id
todo_list.innerText = `${todos.title}`
todo_list_date.innerText = `${todos.due}`
todoList.appendChild(todo)
todo.appendChild(todo_list)
todo.appendChild(todo_list_date)
todo.appendChild(delbtn)
todo.appendChild(chkbx)
delbtn.onclick = delTodo
})
}
function addTodo() {
const id = '' + new Date().getTime()
todos.push({
title: todo_title.value,
due: duedate.value,
id: id
})
render()
saveTodoList()
todo_title.value = ''; duedate.value = ''
if (screen.width < 600) {
console.log('condition met');
setTimeout(() => {
new_todo.style = 'background-color: rgb(37 84 238 / 46%);transition: .5s ease-out'
controls.style = 'display: none'
hr.style = 'display:none'
}, 5000);
}
}
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
addTodo()
}
})
function saveTodoList() {
localStorage.setItem('todos', JSON.stringify(todos))
}
new_todo.addEventListener('click', () => {
controls.style = 'display: flex'
new_todo.style = 'background-color: rgb(37 84 238);transition: .5s ease-out'
})
new_todo.style = 'background-color: rgb(37 84 238 /46%);transition: .5s ease-out'