-
Notifications
You must be signed in to change notification settings - Fork 0
/
index6.html
70 lines (56 loc) · 2.19 KB
/
index6.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
<!DOCTYPE html>
<html>
<head>
<title>To do list</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="index1.css" rel="stylesheet" />
</head>
<body>
<div>
<!--Header with input-->
<div class="header">
<input placeholder="add a task" class="search-input" />
<button class="btn-add">+</button>
</div>
<!--list of tasks-->
<div class="task-list"></div>
</div>
<script>
const searchInput = document.getElementsByClassName("search-input")[0]; //added [0] since it returns an array, we want the first elment
// console.log("[Here is our input]");
// console.log(searchInput);
let taskText;
searchInput.addEventListener("input", e => {
// console.log(e.target.value);
taskText = e.target.value;
});
const btnAdd = document.getElementsByClassName("btn-add")[0];
// Get the task-list div
const taskList = document.getElementsByClassName("task-list")[0];
btnAdd.addEventListener("click", e => {
console.log("[Add this task:]");
console.log(taskText);
// create the task div
let taskDiv = document.createElement("div");
// add the "task" class name to the task div
taskDiv.classList.add("task");
// create the task-name div, add the class name
let taskNameDiv = document.createElement("div");
taskNameDiv.classList.add("task-name");
// create task div content, which the name the inputted task
let taskTextNode = document.createTextNode(taskText);
// create the del-btn button, add the content and its class
let taskDelBtn = document.createElement("button");
taskDelBtn.innerHTML = "X";
taskDelBtn.classList.add("del-btn");
// append the taskTextNode to the taskName Div
taskNameDiv.appendChild(taskTextNode);
// append taskNameDiv and taskDeltBtn to the task div
taskDiv.appendChild(taskNameDiv);
taskDiv.appendChild(taskDelBtn);
// add the task div to the task list div
taskList.insertBefore(taskDiv, taskList.childNodes[0]);
});
</script>
</body>
</html>