-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (69 loc) · 2.52 KB
/
Copy pathscript.js
File metadata and controls
86 lines (69 loc) · 2.52 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
83
84
85
86
"use strict"; // Don't remove it.
// Create app below this line.
let rootDiv = document.getElementById("root");
//Create Input DIV
let inputDiv = document.createElement("div");
inputDiv.id = "inputDiv";
document.getElementById("root").appendChild(inputDiv);
//Create h2 tag
let h2 = document.createElement("h2");
h2.innerHTML = "MY TODO LIST";
document.getElementById("inputDiv").appendChild(h2);
let totalTasks = document.createElement("span");
totalTasks.className = "span";
totalTasks.style.fontSize = "10";
totalTasks.innerHTML = "";
h2.appendChild(totalTasks);
//Create input field and submit button container div
let inputContainer = document.createElement("div");
inputContainer.id = "inputContainer";
document.getElementById("inputDiv").appendChild(inputContainer);
//create ipput field
let txt = document.createElement("input");
txt.id = "add-todo";
txt.className = "add-todo";
txt.type = "text";
txt.placeholder = "Add your item";
document.getElementById("inputContainer").appendChild(txt);
//create add button
let btn = document.createElement("input");
btn.id = "add-btn";
btn.className = "add-btn";
btn.type = "button";
btn.value = "Add";
document.getElementById("inputContainer").appendChild(btn);
//DISPLAY LIST DIV
let listdiv = document.createElement("div");
listdiv.id = "listDiv";
document.getElementById("root").appendChild(listdiv);
// Create UL
let ul = document.createElement("ul");
ul.id = "ul";
ul.style.color = "FFFFFF";
document.getElementById("listDiv").appendChild(ul);
//Add new Li when Add button is clicked
document.getElementById("add-btn").addEventListener("click", addLiText);
//Add new Li when enter key is pressed
document.getElementById("add-todo").addEventListener("keypress", e => e.keyCode === 13 && addLiText());
let countTasks = ul.childElementCount;
function addLiText() {
let inputText = document.getElementById("add-todo").value;
if (inputText === ' ' || inputText === '') {
alert("You must write something!");
} else {
let li = document.createElement("li");
let liText = document.createTextNode(inputText);
li.appendChild(liText);
ul.insertBefore(li, ul.childNodes[0]);
totalTasks.innerHTML = `(${ul.childElementCount})`
document.getElementById("add-todo").value = "";
let deleteBtn = document.createElement("span");
deleteBtn.innerHTML = "X";
deleteBtn.className = "deleteLi";
li.appendChild(deleteBtn);
deleteBtn.addEventListener("click", function() {
ul.removeChild(li);
return totalTasks.innerHTML = `(${ul.childElementCount})`
});
}
}