Final project: a to-do list app that remembers your tasks
Bring the JavaScript course together: build a to-do app that adds, completes, deletes and filters tasks, validates input, saves to localStorage and works with a keyboard, with rubric and full solution.
🎯 By the end of this project you will be able to:
- Plan an app around its data and one render function
- Handle form, click and change events, including event delegation
- Save and load data safely and keep the app accessible
Outline
The brief
A to-do list is the classic first app, because it uses nearly every core skill: data in an array of objects, a render function, forms and validation, clicks, filtering and saving. You will build one that you can really use every day.
Your app must:
- Add a task from a form, refusing empty or space-only tasks with a clear message.
- Show each task with a checkbox (to mark it done), its text as the checkbox's label, and a Delete button.
- Show how many tasks are left, and filter buttons for All, Active and Done.
- Clear all completed tasks with one button.
- Save after every change and load the saved list when the page opens, without crashing if the saved data is missing or broken.
- Work fully with the keyboard, with visible focus and screen-reader-friendly labels.
Set up
Create a folder called todo-app with three empty files. Open index.html in your browser and keep the console open while you work.
todo-app/
├── index.html
├── styles.css
└── script.jsPlan the data first
Before writing any code, decide what the data looks like. Each task is an object; the list is an array:
| Property | Example | Why |
|---|---|---|
id | 3 | A unique number, so we can find the right task even if two have the same text |
text | "Call Ravi" | What to show |
done | false | Ticked or not |
Milestones
| # | Milestone | Skills (lesson) |
|---|---|---|
| 1 | Write index.html: a form with a labelled text box and Add button, three filter buttons, an empty list, a count and a Clear completed button | HTML & CSS course |
| 2 | Style it in styles.css: centred card, comfortable spacing, visible focus, a done style with a line through | HTML & CSS course |
| 3 | In script.js, create tasks and write render() that draws them with a checkbox, label and Delete button | 8, 11 |
| 4 | Handle the form's submit: trim, validate, push a new task with the next id, render, reset | 9, 10 |
| 5 | Use event delegation on the list: change for checkboxes, click for Delete buttons | 9 |
| 6 | Add the filters (with aria-pressed), the tasks-left count and Clear completed | 7, 11 |
| 7 | Save after every change and load safely on start | 12 |
| 8 | Test everything in the checklist below, with the mouse and with only the keyboard | All |
Worked solution: index.html and styles.css
Try each milestone yourself first. Then compare with the worked solution. Different code that passes the same tests is just as good.
Guided exercise
Write index.html
Build the page structure for milestone 1.
- Link
styles.cssin the head, andscript.jswithdefer. - Add a form with
novalidate, a label and text box, an Add submit button and an empty error paragraph linked witharia-describedby. - Add three filter buttons with
data-filtervalues andaria-pressed. - Add an empty
<ul>for the tasks, a count paragraph witharia-live, and a Clear completed button.
Show the solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My tasks</title>
<meta name="description" content="A simple to-do list that remembers your tasks in your browser.">
<link rel="stylesheet" href="styles.css">
<script src="script.js" defer></script>
</head>
<body>
<main class="app">
<h1>My tasks</h1>
<form id="new-task" novalidate>
<label for="task-text">New task</label>
<div class="row">
<input type="text" id="task-text" autocomplete="off" aria-describedby="task-error">
<button type="submit">Add</button>
</div>
<p id="task-error" class="error"></p>
</form>
<div id="filters" class="filters" role="group" aria-label="Show tasks">
<button type="button" data-filter="all" aria-pressed="true">All</button>
<button type="button" data-filter="active" aria-pressed="false">Active</button>
<button type="button" data-filter="done" aria-pressed="false">Done</button>
</div>
<ul id="task-list" class="tasks"></ul>
<div class="footer">
<p id="count" aria-live="polite"></p>
<button type="button" id="clear-done">Clear completed</button>
</div>
</main>
</body>
</html>How to get there: The page holds no tasks: JavaScript draws them. The filter buttons store which view they choose in data-filter, and aria-pressed tells screen readers which one is on. The count uses aria-live so changes are announced.
Guided exercise
Write styles.css
Style the app for milestone 2.
- Use
box-sizing: border-box, a centred white card withmax-width, and a flex row for the text box and Add button. - Make the pressed filter button look selected with
button[aria-pressed="true"]. - Give each task a flex row and a
.task.done labelstyle with a line through and grey text.
Show the solution
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: "Segoe UI", Arial, sans-serif;
line-height: 1.5;
color: #1f2937;
background-color: #f1f5f9;
}
.app {
max-width: 32rem;
margin: 1.5rem auto;
padding: 1.25rem;
background-color: white;
border-radius: 1rem;
box-shadow: 0 4px 16px rgb(0 0 0 / 0.08);
}
h1 {
margin-top: 0;
}
.row {
display: flex;
gap: 0.5rem;
margin-top: 0.25rem;
}
input[type="text"] {
flex: 1;
min-width: 0;
padding: 0.6rem;
border: 2px solid #6b7280;
border-radius: 0.5rem;
font: inherit;
}
button {
padding: 0.5rem 0.9rem;
border: 2px solid #1d4ed8;
border-radius: 0.5rem;
background-color: white;
color: #1d4ed8;
font: inherit;
font-weight: 600;
cursor: pointer;
}
button[type="submit"],
button[aria-pressed="true"] {
background-color: #1d4ed8;
color: white;
}
button:focus-visible,
input:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: 2px;
}
.error {
color: #b91c1c;
font-weight: 700;
min-height: 1.5em;
margin: 0.25rem 0;
}
.filters {
display: flex;
gap: 0.5rem;
}
.tasks {
list-style: none;
padding: 0;
}
.task {
display: flex;
align-items: center;
gap: 0.6rem;
padding: 0.6rem 0;
border-bottom: 1px solid #e5e7eb;
}
.task label {
flex: 1;
}
.task.done label {
color: #6b7280;
text-decoration: line-through;
}
.delete {
border-color: #b91c1c;
color: #b91c1c;
}
.empty {
color: #4b5563;
font-style: italic;
}
.footer {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 0.5rem;
}How to get there: Styling the pressed state with the aria-pressed attribute keeps the look and the accessibility information in step: one attribute drives both. The min-width: 0 on the text box lets it shrink on narrow phones inside the flex row.
Worked solution: script.js
Here is the complete script, running live. Add some tasks, tick them, filter, delete and clear. Everything works with the keyboard too: Tab to move, Space to tick, Enter to add.
Your turn
Write script.js
Make the app work: milestones 3 to 7.
Show the solution
const STORAGE_KEY = "todo-tasks";
const form = document.querySelector("#new-task");
const input = document.querySelector("#task-text");
const error = document.querySelector("#task-error");
const list = document.querySelector("#task-list");
const count = document.querySelector("#count");
const filters = document.querySelector("#filters");
const clearDone = document.querySelector("#clear-done");
let tasks = loadTasks();
let filter = "all";
// ---- Data -------------------------------------------------------------
function loadTasks() {
try {
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "[]");
return Array.isArray(saved) ? saved : [];
} catch {
return [];
}
}
function saveTasks() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(tasks));
}
function visibleTasks() {
if (filter === "active") return tasks.filter((task) => !task.done);
if (filter === "done") return tasks.filter((task) => task.done);
return tasks;
}
// ---- Drawing the page -------------------------------------------------
function render() {
list.replaceChildren();
for (const task of visibleTasks()) {
const li = document.createElement("li");
li.className = task.done ? "task done" : "task";
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = `task-${task.id}`;
checkbox.checked = task.done;
checkbox.dataset.id = task.id;
const label = document.createElement("label");
label.htmlFor = checkbox.id;
label.textContent = task.text;
const remove = document.createElement("button");
remove.type = "button";
remove.className = "delete";
remove.dataset.id = task.id;
remove.textContent = "Delete";
remove.setAttribute("aria-label", `Delete ${task.text}`);
li.append(checkbox, label, remove);
list.append(li);
}
if (list.children.length === 0) {
const li = document.createElement("li");
li.className = "empty";
li.textContent = tasks.length === 0 ? "No tasks yet. Add your first one above." : "No tasks in this view.";
list.append(li);
}
const left = tasks.filter((task) => !task.done).length;
count.textContent = left === 1 ? "1 task left" : `${left} tasks left`;
for (const button of filters.querySelectorAll("button")) {
button.setAttribute("aria-pressed", String(button.dataset.filter === filter));
}
}
// ---- Events -----------------------------------------------------------
form.addEventListener("submit", (event) => {
event.preventDefault();
const text = input.value.trim();
if (!text) {
error.textContent = "Please type a task first.";
input.focus();
return;
}
error.textContent = "";
const nextId = tasks.reduce((max, task) => Math.max(max, task.id), 0) + 1;
tasks.push({ id: nextId, text, done: false });
saveTasks();
render();
form.reset();
input.focus();
});
list.addEventListener("change", (event) => {
if (event.target.type !== "checkbox") return;
const task = tasks.find((t) => t.id === Number(event.target.dataset.id));
task.done = event.target.checked;
saveTasks();
render();
// Keep keyboard focus on the same checkbox after the list is redrawn.
document.getElementById(event.target.id)?.focus();
});
list.addEventListener("click", (event) => {
const button = event.target.closest(".delete");
if (!button) return;
tasks = tasks.filter((task) => task.id !== Number(button.dataset.id));
saveTasks();
render();
input.focus();
});
filters.addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button) return;
filter = button.dataset.filter;
render();
});
clearDone.addEventListener("click", () => {
tasks = tasks.filter((task) => !task.done);
saveTasks();
render();
});
render();Show the HTML and CSS this script works with
HTML
<main class="app">
<h1>My tasks</h1>
<form id="new-task" novalidate>
<label for="task-text">New task</label>
<div class="row">
<input type="text" id="task-text" autocomplete="off" aria-describedby="task-error">
<button type="submit">Add</button>
</div>
<p id="task-error" class="error"></p>
</form>
<div id="filters" class="filters" role="group" aria-label="Show tasks">
<button type="button" data-filter="all" aria-pressed="true">All</button>
<button type="button" data-filter="active" aria-pressed="false">Active</button>
<button type="button" data-filter="done" aria-pressed="false">Done</button>
</div>
<ul id="task-list" class="tasks"></ul>
<div class="footer">
<p id="count" aria-live="polite"></p>
<button type="button" id="clear-done">Clear completed</button>
</div>
</main>CSS
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: "Segoe UI", Arial, sans-serif;
line-height: 1.5;
color: #1f2937;
background-color: #f1f5f9;
}
.app {
max-width: 32rem;
margin: 1.5rem auto;
padding: 1.25rem;
background-color: white;
border-radius: 1rem;
box-shadow: 0 4px 16px rgb(0 0 0 / 0.08);
}
h1 {
margin-top: 0;
}
.row {
display: flex;
gap: 0.5rem;
margin-top: 0.25rem;
}
input[type="text"] {
flex: 1;
min-width: 0;
padding: 0.6rem;
border: 2px solid #6b7280;
border-radius: 0.5rem;
font: inherit;
}
button {
padding: 0.5rem 0.9rem;
border: 2px solid #1d4ed8;
border-radius: 0.5rem;
background-color: white;
color: #1d4ed8;
font: inherit;
font-weight: 600;
cursor: pointer;
}
button[type="submit"],
button[aria-pressed="true"] {
background-color: #1d4ed8;
color: white;
}
button:focus-visible,
input:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: 2px;
}
.error {
color: #b91c1c;
font-weight: 700;
min-height: 1.5em;
margin: 0.25rem 0;
}
.filters {
display: flex;
gap: 0.5rem;
}
.tasks {
list-style: none;
padding: 0;
}
.task {
display: flex;
align-items: center;
gap: 0.6rem;
padding: 0.6rem 0;
border-bottom: 1px solid #e5e7eb;
}
.task label {
flex: 1;
}
.task.done label {
color: #6b7280;
text-decoration: line-through;
}
.delete {
border-color: #b91c1c;
color: #b91c1c;
}
.empty {
color: #4b5563;
font-style: italic;
}
.footer {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 0.5rem;
}Live preview: try it
Console
Nothing logged yet.
How to get there: The script is organised in three parts: data (load, save, filter), drawing (render), and events. Every event handler follows the same steps: change tasks, save, render. Event delegation means the list needs just two listeners, however many tasks it holds. The ?? "[]", try/catch and Array.isArray check make loading safe on a first visit or with broken data. Each Delete button has an aria-label that names its task, so a screen-reader user hears *Delete Call Ravi*, not just *Delete*.
Test checklist
| Test | Expected |
|---|---|
| Press Add with an empty box, or only spaces | *Please type a task first.* appears and focus returns to the box |
| Add three tasks, using Enter for at least one | Three tasks appear and *3 tasks left* shows |
| Tick a task | It is crossed out and the count drops by one |
| Click Done, then Active, then All | Only matching tasks show; the pressed button looks selected |
| Delete a task | It disappears and the count updates |
| Clear completed | Every ticked task is removed |
| Refresh the page | Everything is still there |
In the console run localStorage.setItem("todo-tasks", "oops"), then refresh | The app starts with an empty list instead of crashing |
| Do all of the above using only the keyboard | Every control is reachable with Tab and has a visible focus outline |
Ideas to go further
- Let people edit a task's text by double-clicking it (and with a keyboard-friendly Edit button).
- Add a due date with
<input type="date">and sort tasks by date. - Show *All done!* with a small celebration when the last task is ticked.
- Publish your app on GitHub Pages (HTML & CSS lesson 13) and use it on your phone.
Recap
- You designed the data first, then drew the page from it with one render function.
- Every action follows one rule: change the data, save it, render.
- Event delegation handled checkboxes and Delete buttons for any number of tasks.
- Validation, safe loading, labels, aria-pressed and focus handling made the app robust and accessible.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Why does each task have an id as well as text?
2. Why are there only two listeners on the list, however many tasks there are?
3. What does every action in the app do, in order?
4. Why give each Delete button an aria-label like "Delete Call Ravi"?
Self-check and complete the course
Tick every rubric item to complete the project.