-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (53 loc) · 1.76 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
let inputField = document.getElementById('inputField');
let addTodo = document.getElementById('addTodo');
let todoContainer = document.getElementById('todoContainer');
addTodo.addEventListener('click', () => {
const str = inputField.value.trim();
if(str.length == 0){
alert('You must enter a task!');
return;
}
let taskFinished = false;
const paragraph = document.createElement('p');
const finishTodo = document.createElement('button');
const delTodo = document.createElement('button');
const div = document.createElement('div');
const br = document.createElement('br');
div.classList.add('actions')
finishTodo.id = 'finish-btn';
finishTodo.innerText = 'Finish';
delTodo.id = 'delete-btn';
delTodo.innerText = 'Delete';
paragraph.innerText = inputField.value;
paragraph.classList.add('todo-item');
todoContainer.appendChild(paragraph);
div.appendChild(finishTodo);
div.appendChild(delTodo);
todoContainer.appendChild(div);
todoContainer.appendChild(br);
inputField.value = '';
finishTodo.addEventListener('click', () => {
if(!taskFinished){
paragraph.style.textDecoration = 'line-through';
taskFinished = true;
finishTodo.innerText = 'Undo';
} else{
paragraph.style.textDecoration = 'none';
taskFinished = false;
finishTodo.innerText = 'Finish'
}
});
delTodo.addEventListener('click', () => {
todoContainer.removeChild(paragraph);
todoContainer.removeChild(div);
});
});
inputField.addEventListener("keypress", (event) => {
// If the user presses the "Enter" key on the keyboard
if (event.key === "Enter") {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
addTodo.click();
}
});