Project: build a to-do list in the browser
Course contents (16 lessons)
- Getting started
- What is JavaScript and where does it run?
- Variables with let and const
- Values and operators
- Data types: numbers, strings, booleans, null and undefined
- Operators: arithmetic, comparison and logical
- Strings and template literals
- Making decisions and repeating work
- Conditionals: if, else and switch
- Loops: for, while and for...of
- Functions
- Functions: declarations, parameters and return values
- Arrow functions and callbacks
- Arrays and objects
- Arrays: creating, reading and changing lists
- Array methods: map, filter, find and reduce
- Objects: properties, methods and copying
- Scope and the browser
- Scope and closures, gently
- The DOM: selecting and changing elements
- Events: reacting to clicks, input and forms
- Final project
- Project: build a to-do list in the browser
What we are building
A to-do list where you can add a task, tick it off, delete it and filter between all, active and done tasks. It will remember your tasks when you reload the page, using the browser's local storage. The project uses almost everything from this course: variables, arrays and objects, functions, array methods, DOM and event delegation.
Create a folder with three files: index.html, style.css and app.js.
Step 1: the HTML
<!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>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<h1>My tasks</h1>
<form id="task-form">
<input id="task-input" placeholder="What needs doing?" autocomplete="off" required>
<button type="submit">Add</button>
</form>
<div id="filters">
<button data-filter="all" class="active">All</button>
<button data-filter="active">Active</button>
<button data-filter="done">Done</button>
</div>
<ul id="task-list"></ul>
<p id="summary"></p>
</main>
<script src="app.js"></script>
</body>
</html>
The list is empty. JavaScript will fill it from the data. The data-filter attributes let us know which button was pressed.
Step 2: a little CSS
body { font-family: system-ui, sans-serif; background: #f4f5f7; margin: 0; }
main { max-width: 480px; margin: 40px auto; padding: 0 16px; }
form { display: flex; gap: 8px; }
input { flex: 1; padding: 10px; font-size: 1rem; }
button { padding: 10px 14px; cursor: pointer; }
#filters { margin: 12px 0; display: flex; gap: 6px; }
#filters .active { background: #222; color: #fff; }
ul { list-style: none; padding: 0; }
li { display: flex; justify-content: space-between; align-items: center;
background: #fff; padding: 10px; margin-bottom: 6px; border-radius: 4px; }
li.done span { text-decoration: line-through; color: #888; }
Step 3: the data and the render function
The central idea is to keep the data in one place, an array, and have a function draw the list from it. We never edit the page piece by piece. We change the data, then call render().
// app.js
const form = document.querySelector("#task-form");
const input = document.querySelector("#task-input");
const list = document.querySelector("#task-list");
const filters = document.querySelector("#filters");
const summary = document.querySelector("#summary");
let tasks = loadTasks();
let currentFilter = "all";
function loadTasks() {
try {
return JSON.parse(localStorage.getItem("tasks")) ?? [];
} catch {
return [];
}
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function visibleTasks() {
if (currentFilter === "active") return tasks.filter(t => !t.done);
if (currentFilter === "done") return tasks.filter(t => t.done);
return tasks;
}
function render() {
list.innerHTML = "";
for (const task of visibleTasks()) {
const li = document.createElement("li");
li.dataset.id = task.id;
li.classList.toggle("done", task.done);
const text = document.createElement("span");
text.textContent = task.text;
const remove = document.createElement("button");
remove.textContent = "Delete";
remove.dataset.action = "delete";
li.append(text, remove);
list.append(li);
}
const left = tasks.filter(t => !t.done).length;
summary.textContent = `${left} of ${tasks.length} tasks left`;
}
form.addEventListener("submit", event => {
event.preventDefault();
const text = input.value.trim();
if (!text) return;
tasks.push({ id: Date.now(), text, done: false });
saveTasks();
render();
form.reset();
input.focus();
});
list.addEventListener("click", event => {
const li = event.target.closest("li");
if (!li) return;
const id = Number(li.dataset.id);
if (event.target.dataset.action === "delete") {
tasks = tasks.filter(t => t.id !== id);
} else {
const task = tasks.find(t => t.id === id);
task.done = !task.done;
}
saveTasks();
render();
});
filters.addEventListener("click", event => {
const button = event.target.closest("button[data-filter]");
if (!button) return;
currentFilter = button.dataset.filter;
for (const b of filters.children) {
b.classList.toggle("active", b === button);
}
render();
});
render();
How it works
The data. tasks is an array of objects such as { id: 1718000000000, text: "Buy milk", done: false }. The id comes from Date.now(), which is a number of milliseconds and is unique enough for a personal list.
Loading and saving. localStorage only stores strings, so we use JSON.stringify and JSON.parse. On first visit getItem returns null, and ?? [] gives us an empty array. The try/catch protects us if the stored text is damaged.
Rendering. render() clears the list and rebuilds it from visibleTasks(). We use createElement and textContent so a task like <b>hi</b> shows as text and cannot inject markup.
Adding. On submit we stop the page reload, trim the text, ignore empty input, push a new object, save and render.
Completing and deleting. We listen once on the ul. closest("li") tells us which task was clicked, and its data-id identifies the object. If the click was on the Delete button we filter that task out. Otherwise we flip its done flag. This is event delegation, so it works for tasks created later.
Filters. The filter buttons change currentFilter and call render() again. The array itself is untouched.
Common mistakes
- Editing the DOM and forgetting the data. If you only change the page, a re-render brings the old state back. Change the data first.
- Forgetting to save. Call
saveTasks()after every change. - Comparing id types.
dataset.idis a string andtask.idis a number. That is why we convert withNumber. - Using innerHTML with the task text. It opens the door to XSS.
- Opening the file in a way that blocks storage. Some browsers restrict local storage for
file://pages. Use Live Server if it does not work.
Try it yourself
- Add a "Clear completed" button that removes every done task.
- Let the user double-click a task to edit its text.
- Add a due-date field and sort the list by date.
- When you finish, read the code again and rename anything that is unclear. Then move on to the follow-up course on modern JavaScript, which covers modules, promises and fetch.
// app.js
const form = document.querySelector("#task-form");
const input = document.querySelector("#task-input");
const list = document.querySelector("#task-list");
const filters = document.querySelector("#filters");
const summary = document.querySelector("#summary");
let tasks = loadTasks();
let currentFilter = "all";
function loadTasks() {
try {
return JSON.parse(localStorage.getItem("tasks")) ?? [];
} catch {
return [];
}
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function visibleTasks() {
if (currentFilter === "active") return tasks.filter(t => !t.done);
if (currentFilter === "done") return tasks.filter(t => t.done);
return tasks;
}
function render() {
list.innerHTML = "";
for (const task of visibleTasks()) {
const li = document.createElement("li");
li.dataset.id = task.id;
li.classList.toggle("done", task.done);
const text = document.createElement("span");
text.textContent = task.text;
const remove = document.createElement("button");
remove.textContent = "Delete";
remove.dataset.action = "delete";
li.append(text, remove);
list.append(li);
}
const left = tasks.filter(t => !t.done).length;
summary.textContent = `${left} of ${tasks.length} tasks left`;
}
form.addEventListener("submit", event => {
event.preventDefault();
const text = input.value.trim();
if (!text) return;
tasks.push({ id: Date.now(), text, done: false });
saveTasks();
render();
form.reset();
input.focus();
});
list.addEventListener("click", event => {
const li = event.target.closest("li");
if (!li) return;
const id = Number(li.dataset.id);
if (event.target.dataset.action === "delete") {
tasks = tasks.filter(t => t.id !== id);
} else {
const task = tasks.find(t => t.id === id);
task.done = !task.done;
}
saveTasks();
render();
});
filters.addEventListener("click", event => {
const button = event.target.closest("button[data-filter]");
if (!button) return;
currentFilter = button.dataset.filter;
for (const b of filters.children) {
b.classList.toggle("active", b === button);
}
render();
});
render();
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account