LESSON 26 · BEGINNER4 min read

Project: Interactive To-Do List

Build a complete browser to-do list with delegated events, filters, accessible markup, and localStorage persistence.

ES2024 / Node.js 20+Free to readModule 5 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Model a small application with one state object and a single render function.
  • Create list items safely and handle add, toggle, delete, and filter actions.
  • Persist tasks in localStorage and recover from missing or corrupted data.
  • Review the interface for keyboard use, screen-reader feedback, and empty states.

01Start with state and markup

A to-do list looks simple until you list its states: every task can be active or completed, the view can show all, active, or completed tasks, and an empty view needs an explanation. Begin by defining one array as the source of truth. Each task is an object with an id, text, and done flag. A separate filter string decides which tasks are visible. The DOM will be rebuilt from this state, never edited ad hoc. Build the project in four passes: state and markup, render, delegated actions, then storage and review.

Build semantic markup next. A form with a label and text input adds tasks. A status paragraph announces the remaining count, an unordered list holds the tasks, and a paragraph explains an empty list. Filter buttons carry data-filter values. Use type='button' for buttons that do not submit, and keep ids and classes stable so the script can find them.

index.html⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02Render from one function

render reads the current state and produces the visible list. Filter the array first, then create one list item per task. Each item needs a checkbox, the task text, and a delete button. Store the task id in a data attribute so event handlers can find the matching object. Replace the list contents in one operation.

Use textContent for the task text, because it is user input. Mark completed tasks with a class rather than a colour inline, and keep the checkbox checked property in sync. Toggle the empty paragraph with the hidden property. Update the status text with the number of unfinished tasks, or a storage warning when saving is unavailable. Even when the list grows, this function gives a predictable result.

app.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Delegate user actions

The submit handler adds a task. Trim the input, ignore an empty string, create an id, push the task, clear the input, save, render, and return focus to the field. Native validation with required also helps, but the trim check prevents a value made only of spaces.

One click listener on the list handles toggles and deletes. Find the nearest element with a data-action, then the list item, then the task by id. For a toggle, read the checkbox's checked property; for a delete, filter the array. Save and render after either change. Filter buttons are separate: set the active filter, update aria-pressed, and render.

app.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Persist and ship

Persistence should be boring. Write a save function that serializes the array inside try/catch, and a load function that parses and validates it. Use a versioned key such as coursepodia:todos:v1 so a future shape can migrate rather than crash. If storage is unavailable, keep the list working in memory and tell the user that changes will not survive a refresh.

app.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

05Review checklist

Review the finished page without a mouse. Add a task with Enter, toggle it with Space, delete it with Enter, and switch filters with Tab and Enter. Refresh to confirm persistence, then corrupt the stored value in DevTools and reload to check the fallback. Confirm that the status message announces changes and that the empty state is clear. Also verify that every Delete button names its task, so screen-reader users know which row will disappear.

LET IT CLICK

The ideas worth keeping.

  • Keep one state array and one render function; handlers change state instead of editing the DOM in several places.
  • Use delegated events, data attributes, and textContent to support dynamic rows safely.
  • Persist defensively with a versioned key, validation on load, and visible status feedback.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Clear completed tasks

Add a button with id clear-done to the project. Write its click handler so it removes every completed task, saves, re-renders, and then announces how many tasks were cleared in the status element. If no task is completed, it should do nothing.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const clearButton = document.querySelector("#clear-done");clearButton.addEventListener("click", () => {  const completed = todos.filter((todo) => todo.done).length;  if (completed === 0) return;  todos = todos.filter((todo) => !todo.done);  save();  render();  status.textContent = "Cleared " + completed + " completed task(s)";});

The handler counts first so it can report the result, then replaces the array with only active tasks. Saving before rendering keeps storage and the visible list in step, and the final status text gives immediate feedback.

02

Exercise 2: Rename a task safely

Write renameTodo(id, nextText) for the project. Trim the new text, reject empty text, find the task by id, update its text, save, and re-render. Return true on success and false when the text is empty or the id is unknown.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
function renameTodo(id, nextText) {  const text = nextText.trim();  if (!text) return false;  const todo = todos.find((entry) => entry.id === id);  if (!todo) return false;  todo.text = text;  save();  render();  return true;}

Guards run before mutation, so an invalid rename leaves the state untouched. The boolean result lets the caller keep the edit form open when the update fails.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01Why does the project keep todos in one array and rebuild the list with render?
02How should a task's text be placed into its list item?
03What should happen when localStorage.setItem throws?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Clear completed tasks

Add a button with id clear-done to the project. Write its click handler so it removes every completed task, saves, re-renders, and then announces how many tasks were cleared in the status element. If no task is completed, it should do nothing.

main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

MODULE 05 · PRACTICAL SELF-CHECK

Pause. Put the pieces together.

You’ve reached the end of DOM & Browser Basics. Before moving on, use this task to check what you can do independently.

Build a persistent to-do list with add, complete, delete, filter, empty states, and keyboard-accessible controls.

Try it in your own environment and write down the result. This is a self-check; reading completion does not evaluate your code.

Review this module

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.