-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
126 lines (104 loc) · 4.36 KB
/
todo.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This function contains the action for inputting text, submitting the task,
giving each new element specific attributes, clearing the task from the current
tasks.
*/
function actionForSubmitting(){
document.getElementById('submit').onclick = function(){
let newResponse = String(document.getElementById('inputField').value)
if(!/\S/.test(newResponse)){
alert('You must enter a task.')
document.getElementById('inputField').value = ''
}else{
document.getElementById('inputField').value = ''
let newDiv = document.createElement('div')
let newH4 = document.createElement('h4')
let newInputBtn = document.createElement('input')
newDiv.setAttribute('class', 'divContainer')
newH4.setAttribute('class', 'h4Container')
newInputBtn.setAttribute('type', 'checkbox')
newInputBtn.setAttribute('class', 'myCheckBox')
textForH4 = document.createTextNode(newResponse)
newH4.appendChild(textForH4)
newDiv.appendChild(newInputBtn)
newDiv.appendChild(newH4)
newDiv.style.display = 'flex';
newDiv.style.flexDirection = 'row';
newDiv.style.justifyContent = 'left';
newDiv.style.marginTop = '-40px';
newDiv.style.whiteSpace = 'normal';
let appendSpot = document.getElementById('toDoSaved')
appendSpot.appendChild(newDiv)
let checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(checkbox => {
checkbox.addEventListener('click', () => {
let parentDiv = checkbox.closest('div');
let childElement = parentDiv.querySelector('h4');
if(checkbox.checked){
childElement.style.textDecoration = 'line-through';
}else{
childElement.style.textDecoration = 'none'
}
});
})
let boxClicked = document.querySelectorAll('input[type="checkbox"]')
boxClicked.forEach(boxCheckbox => {
boxCheckbox.addEventListener('click', () => {
var parentDiv = boxCheckbox.closest('div')
var newDivSpot = document.getElementById('completedSaved')
var oldDivSpot = document.getElementById('toDoSaved')
//var oldParentDiv = oldDivSpot.closest('div')
if(boxCheckbox){
parentDiv.remove()
newDivSpot.appendChild(parentDiv)
parentDiv.firstChild.setAttribute('disabled', 'disabled')
}
})
})
}}
}
function clearCompleted(){
document.getElementById('clearCompletedTask').onclick = function(){
let taskEntries = document.getElementById('completedSaved')
if(taskEntries.firstChild){
if(confirm('WARNING: Are you sure you want to clear your completed tasks?')){
while(taskEntries.firstChild){
taskEntries.removeChild(taskEntries.lastChild)
}
}
}else{
alert('There is nothing to reset.')
}
}
}
function resetButton(){
document.getElementById('reset').onclick = function(){
let myNode = document.getElementById('toDoSaved')
let myNodeComplete = document.getElementById('completedSaved')
if( myNode.firstChild == null && myNodeComplete.firstChild == null){
alert('There is nothing to reset.')
}else {
if(confirm('WARNING: Are you sure you want to clear the list?') == true){
while( myNode.firstChild){
myNode.removeChild(myNode.lastChild)
}
while( myNodeComplete.firstChild){
myNodeComplete.removeChild(myNodeComplete.lastChild)
}
}
}
}
}
function enterKey(){
const textInput = document.querySelector('#inputField')
const buttonSubmit = document.querySelector('#submit')
textInput.addEventListener('keyup', event => {
if (event.key === 'Enter'){
event.preventDefault();
buttonSubmit.click();
}
})
}
actionForSubmitting()
resetButton()
enterKey()
clearCompleted()