-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (72 loc) · 2.35 KB
/
script.js
File metadata and controls
89 lines (72 loc) · 2.35 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
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
const class_names = {
TODO_ITEM: 'todo-container',
TODO_CHECKBOX: 'todo-checkbox',
TODO_TEXT: 'todo-text',
TODO_DELETE: 'todo-delete',
}
const list_elements = document.getElementById('todo-list')
const item_count_element = document.getElementById('item-count')
const unchecked_count_element = document.getElementById('unchecked-count')
let item_count = 0
let unchecked_count = 0
function new_todo() {
create_new_todo()
}
function create_new_todo () {
//gets user input
let user_input = document.getElementById(class_names.TODO_TEXT).value;
//checks to see if input is valid
if (!user_input) {
alert('please input a value')
return ''
}
// makes a list object and puts the user input in it
let new_list_element = document.createElement("todo-container")
let b = document.createElement('br')
// clears input box
document.getElementById(class_names.TODO_TEXT).value = ''
// changes the counts in the top span
item_count++
unchecked_count++
// adds a check box to the list item
let checkbox = document.createElement('input')
checkbox.type = 'checkbox'
checkbox.class = class_names.TODO_CHECKBOX
checkbox.name = 'checkbox'
checkbox.id = 'checkboxID'
checkbox.onclick = (event) => {this.complete_todo(event)}
// adds a delete button to the list item
let deleter = document.createElement('input')
deleter.type = 'button'
deleter.style.float = 'right'
deleter.value = 'remove this item'
deleter.onclick = (event) => {this.delete_todo(event)}
// adds text and checkbox to the list elements
new_list_element.appendChild(checkbox)
new_list_element.appendChild(document.createTextNode(user_input))
new_list_element.appendChild(deleter)
list_elements.appendChild(new_list_element)
// sets top span values
item_count_element.innerHTML = item_count
unchecked_count_element.innerHTML = unchecked_count
}
function complete_todo (event) {
let eChecked = event.target
if (eChecked.checked) {
unchecked_count--
} else {
unchecked_count++
}
unchecked_count_element.innerHTML = unchecked_count
}
function delete_todo (event) {
let eClicked = event.target
let parent_node = eClicked.parentNode
list_elements.removeChild(parent_node)
item_count--
if (unchecked_count > 0){
unchecked_count--
}
unchecked_count_element.innerHTML = unchecked_count
item_count_element.innerHTML = item_count
}