JavaScript project
To-Do List
Build a task list where you can add tasks, mark them complete, and delete them.
- Arrays
- DOM manipulation
- Events
How to run this project
- Create a file named
index.htmlin a new folder. - Copy the complete code below into the file and save it.
- Open
index.htmlin your browser. No installation or API key is needed.
Complete project code
This single file includes the HTML interface, basic CSS styling, and JavaScript behavior. The script runs after the HTML so all the elements are available.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>To-Do List</title>
<style>
body { font-family: system-ui, sans-serif; max-width: 640px; margin: 40px auto; padding: 0 20px; line-height: 1.6; }
form { display: grid; gap: 12px; }
input, select, button { font: inherit; padding: 8px; }
button { cursor: pointer; }
button:disabled { cursor: default; }
li { margin: 12px 0; overflow-wrap: anywhere; }
li button { margin-left: 12px; }
input[type="checkbox"] { margin-right: 8px; }
#answers { display: grid; gap: 8px; }
</style>
</head>
<body>
<h1>To-Do List</h1>
<form id="task-form">
<label for="task-input">New task</label>
<input id="task-input" maxlength="120" required>
<button type="submit">Add task</button>
</form>
<p id="task-count" role="status"></p>
<ul id="task-list"></ul>
<script>
const form = document.querySelector("#task-form");
const input = document.querySelector("#task-input");
const list = document.querySelector("#task-list");
const count = document.querySelector("#task-count");
let tasks = [];
let nextId = 1;
function renderTasks() {
list.replaceChildren();
tasks.forEach((task) => {
const item = document.createElement("li");
const label = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = task.completed;
const text = document.createElement("span");
text.textContent = task.title;
text.style.textDecoration = task.completed ? "line-through" : "none";
checkbox.addEventListener("change", () => {
task.completed = checkbox.checked;
text.style.textDecoration = task.completed ? "line-through" : "none";
updateCount();
});
const remove = document.createElement("button");
remove.type = "button";
remove.textContent = "Delete";
remove.setAttribute("aria-label", "Delete " + task.title);
remove.addEventListener("click", () => {
tasks = tasks.filter((entry) => entry.id !== task.id);
renderTasks();
input.focus();
});
label.append(checkbox, text);
item.append(label, remove);
list.append(item);
});
updateCount();
}
function updateCount() {
const remaining = tasks.filter((task) => !task.completed).length;
count.textContent = remaining + " task(s) remaining";
}
form.addEventListener("submit", (event) => {
event.preventDefault();
const title = input.value.trim();
if (!title) return;
tasks.push({ id: nextId++, title, completed: false });
renderTasks();
form.reset();
input.focus();
});
renderTasks();
</script>
</body>
</html>How the code works
1. Select the elements and store tasks
querySelector connects JavaScript to the form, input, list, and counter using their HTML IDs. The tasks array holds objects with an id, title, and completed flag. nextId gives each task a unique number, so tasks with the same title can still be deleted independently.
2. Handle the form submission
The submit listener runs when you click Add task or press Enter. preventDefault stops the browser from reloading the page. trim removes surrounding spaces, and the early return rejects blank tasks. push adds a new task before the form is reset and the input receives focus.
3. Render the task list
renderTasks clears the old list with replaceChildren, then forEach creates a list item for every task. createElement builds real DOM nodes. textContent displays the task as text, so user input is never interpreted as HTML.
4. Complete and delete tasks
The checkbox change listener updates the completed flag and the text decoration. The Delete listener uses filter to keep every task except the one with the matching id, then renders the new list. updateCount counts unfinished tasks and updates the status message.
5. Understand the lifetime of state
This version keeps tasks in memory, so refreshing the page clears them. As a next exercise, save tasks with JSON.stringify and localStorage, then restore and validate them when the page loads.
Try it yourself
Add two tasks, check one off, then delete the other. The remaining count should change from 2 to 1 to 0. Try adding only spaces: no task should appear.
