-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (47 loc) · 1.41 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 date = document.querySelector(".date");
let inp = document.querySelector("#new-task")
let def = document.querySelector(".default")
let addBtn = document.querySelector("#add-task-btn")
let ol = document.querySelector(".tasks");
const currentDate = new Date();
const formattedDate = currentDate.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
date.textContent = formattedDate;
function addTask() {
let newTask = inp.value;
console.log(newTask);
if (newTask) {
def.remove();
}
if (newTask) {
let newli = document.createElement("li")
newli.classList.add("task-item");
let span = document.createElement("span");
span.classList.add("task-text");
span.innerText = newTask;
let delBtn = document.createElement("button");
delBtn.classList.add("delete-btn");
delBtn.innerHTML = "✖";
delBtn.addEventListener("click", () => {
ol.removeChild(newli);
console.log("Task Deleted: ", newTask);
});
newli.appendChild(span);
newli.appendChild(delBtn);
ol.appendChild(newli);
console.log("Task added:", newTask);
}
}
addBtn.addEventListener("click", () => {
addTask();
inp.value = "";
});
inp.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
addTask();
inp.value = "";
}
});