-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (45 loc) · 1.41 KB
/
script.js
File metadata and controls
59 lines (45 loc) · 1.41 KB
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
let arr = {}
function todoItem (inpTxt)
{
this.inpTxt = inpTxt
this.checked = false
this.id = Date.now()
this.htmlElement = '<label for="' + this.id + '">' + this.inpTxt + '</label><input type="checkbox" id ="' + this.id + '" onclick="checkBoxChanged(event)">'
this.htmlElementDone = this.htmlElement.substring(0, this.htmlElement.length - 1) + "checked/>"
}
function addTodoItem(inpTxt)
{
newItem = new todoItem(inpTxt)
arr[newItem.id] = newItem
}
function update()
{
const dispInTodo = document.getElementById('todo')
const dispInDone = document.getElementById('done')
dispInDone.innerHTML = ''
dispInTodo.innerHTML = ''
for (const [key, value] of Object.entries(arr)) {
value.checked ? dispInDone.innerHTML += value.htmlElementDone : dispInTodo.innerHTML += value.htmlElement
}
}
function checkBoxChanged(e)
{
const toChange = document.getElementById(e.srcElement.id);
arr[e.srcElement.id].checked = !arr[e.srcElement.id].checked
update()
}
const form = document.getElementById('formToAddTasks')
form.addEventListener('submit', event => {
event.preventDefault()
//Input field
const input = document.getElementById('todoItem')
//Get input value
const textToAdd = input.value.trim()
//Prevent adding nothing
if (textToAdd !== ''){
addTodoItem(textToAdd)
input.value = ''
input.focus()
}
update()
})