-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto-do-list.html
More file actions
82 lines (67 loc) · 2.9 KB
/
to-do-list.html
File metadata and controls
82 lines (67 loc) · 2.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do List</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>
<div class="container">
<header class="text-center my-4">
<h1>To-Do List</h1>
</header>
<main class="row">
<section class="border border-lg-none p-4 col-lg-4">
<form id="to-do-form">
<h3>
<label class="form-label" for="to-do">Enter to-do:</label>
</h3>
<input class="form-control" type="text" name="to-do" id="to-do">
<div class="d-grid"><button class="btn btn-secondary mt-3 add">Add To-Do</button></div>
</form>
</section>
<section class="my-4 my-lg-0 col-lg-8">
<h3>To-Dos:</h3>
<ul class="list-group" id="to-do-list">
<li class="to-do-item list-group-item d-flex justify-content-between align-items-center">
<p class="m-0">Finish this exercise</p>
<button class="btn btn-danger">Done</button>
</li>
</ul>
</section>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz"
crossorigin="anonymous">
</script>
<script>
//**************
//Get reference of the form
const form = document.getElementById("to-do-form");
//add a to do item when the form is submitted, note: using click vs submit will add item upon clicking input field
form.addEventListener("submit", (event) => {
event.preventDefault();
//value = will present the value that is input in the input field
const todoItemEntered = form.firstElementChild.nextElementSibling.value;
console.log(todoItemEntered);
//creating an li element
const todoLi = document.createElement("li");
// using classlist.add to add a class attribute with bootstrap styling
todoLi.classList.add("to-do-item", "list-group-item", "d-flex", "justify-content-between", "align-items-center");
//populates content inside the targeted element
todoLi.innerHTML = `<p>${todoItemEntered}</p>` + `<button class="btn btn-danger">Done</button>`
// ^^ targeting variable used to get value of the input field
document.getElementById("to-do-list").appendChild(todoLi);
form.firstElementChild.nextElementSibling.value = "";
})
// delete a to-do item when the form is clicked
document.getElementById("to-do-list").addEventListener("click", event => {
if(event.target.classList.contains("btn")) {
event.target.parentElement.remove();
}
})
</script>
</body>
</html>