-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task3.html
107 lines (91 loc) · 3.51 KB
/
Task3.html
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
<!DOCTYPE html>
<!--
How do I run this script?
Locate the .html file in the your GUI file system (Apple Finder or the Windows),
right click, then press OPEN WITH
YOUR OBJECTIVE:
Take the logic we've defiend in the last two tasks and tie them to the parts
of the code that make the most sense. By the end, you should have a simple webpage
that lets you add todos!
Most code is already implemented. Only modify the file under the "// YOUR CODE HERE" comments.
For this file, be sure you understand the sections labeled with "// BE ABLE TO EXPLAIN."
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#todo-list {
margin-top: 20px;
list-style-type: none;
padding: 0;
}
.todo-item {
padding: 10px;
margin: 5px 0;
background-color: #f0f0f0;
border: 1px solid #ccc;
display: flex;
justify-content: space-between;
}
.todo-item button {
margin-left: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Todo List</h1>
<input type="text" id="todo-input" placeholder="Enter a new todo">
<button id="add-todo-btn">Add Todo</button>
<ul id="todo-list"></ul>
<script>
const todoList = [];
// BE ABLE TO EXPLAIN what document.getByElementID() and .addEventListener() does in this context
document.getElementById('add-todo-btn').addEventListener('click', () => {
// BE ABLE TO EXPLAIN what document.getByElementID() does in this context
const todoInput = document.getElementById('todo-input');
const createdTodo = todoInput.value.trim();
// Add non-empty string todos to the todo list
// YOUR CODE HERE
if (createdTodo !== '') {
addTodo(createdTodo)
}
// reset the input value after add todo is clicked.
todoInput.value = '';
});
// This re-renders the list of items displayed on the webpage based on the new todoList
function refreshTodoList() {
// BE ABLE TO EXPLAIN what document.getByElementID() does in this context
const todoListElement = document.getElementById('todo-list');
todoListElement.innerHTML = '';
todoList.forEach((todo, index) => {
const listItem = document.createElement('li');
listItem.className = 'todo-item';
// Each item in the todo list has a button attached to it that has an event that
// deletes the todo using the function you wrote
listItem.innerHTML = `
${todo}
<button onclick="deleteTodo(${index})">Delete</button>
`;
todoListElement.appendChild(listItem);
});
}
function deleteTodo(index) {
// YOUR CODE HERE (recycle your work from former tasks)
todoList.splice(index, 1)
refreshTodoList()
}
function addTodo(todo) {
// YOUR CODE HERE (recycle your work from former tasks)
todoList.push(todo)
refreshTodoList()
}
</script>
</body>
</html>