-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_program_06.html
More file actions
58 lines (58 loc) · 1.41 KB
/
js_program_06.html
File metadata and controls
58 lines (58 loc) · 1.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Dynamic To-Do List</title>
<style>
body {
font-family: Arial,sans-serif;
margin: 40px;
}
li {
margin: 8px 0;
}
.completed {
text-decoration: line-through;
color: gray;
}
button.remove {
margin-left: 10px;
background-color: red;
color: white;
border: none;
padding: 4px 8px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>My To-Do List</h2>
<input type="text" id="taskInput" placeholder="Enter a new task" />
<button onclick="addTask()">Add Task</button>
<ul id="taskList"></ul>
<script>
function addTask() {
const taskText = document.getElementById("taskInput").value.trim();
if (taskText === "") return;
const li = document.createElement("li");
li.textContent = taskText;
// Toggle 'completed' class on click
li.addEventListener("click", function () {
li.classList.toggle("completed");
});
// Create and add Remove button
const removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.className = "remove";
removeBtn.addEventListener("click", function (e) {
e.stopPropagation(); // Prevent toggling when clicking remove
li.remove();
});
li.appendChild(removeBtn);
document.getElementById("taskList").appendChild(li);
document.getElementById("taskInput").value = "";
}
</script>
</body>
</html>