-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (33 loc) · 985 Bytes
/
Copy pathscript.js
File metadata and controls
42 lines (33 loc) · 985 Bytes
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
// 1. Selecting HTML Elements
const form = document.querySelector("form");
const input = document.querySelector("#input_id");
const button = document.querySelector(".btn");
// new UL
const todoList = document.getElementById("todoList");
// 2. Add Click Events
// Add Items
button.addEventListener("click", add_li);
function add_li(e) {
e.preventDefault();
const li = document.createElement("li");
const itemId = Date.now(); // Unique Id
li.setAttribute("data-id", itemId);
li.className = "li_item";
li.innerHTML = input.value;
input.value = "";
//
const dlt = document.createElement("button");
dlt.innerHTML = "Delete";
dlt.classList = "dlt_btn";
//
todoList.appendChild(li);
li.appendChild(dlt);
// Delete Items
dlt.addEventListener("click", dlt_item);
function dlt_item(e) {
e.preventDefault();
// form.removeChild(li);
const listItem = document.querySelector(`[data-id="${itemId}"]`);
todoList.removeChild(listItem);
}
}