Web Development · Module 13 · Lesson 21 min read

Project: Interactive To-Do App

Goal

Build a to-do app with plain HTML, CSS and JavaScript that remembers tasks after the page is closed.

Features

  • Add a task (with Enter or a button)
  • Mark tasks done or undone by tapping them
  • Delete a task
  • Filter: All / Active / Done
  • Show how many tasks are left
  • Save everything in localStorage

Skills you'll practise

Semantic HTML, Flexbox, the DOM, events and event delegation, array methods, and localStorage with JSON.

Steps

  1. HTML: a form with an input and button, a <ul> for tasks, filter buttons.
  2. State: an array of { id, text, done } objects. It's the single source of truth.
  3. render(): clear the list and rebuild it from the state, applying the current filter.
  4. Events: submit adds, clicks on the list toggle or delete (one delegated listener).
  5. Persist: after every change, save() to localStorage; on load, read it back.

Extensions

Edit by double-clicking, due dates, drag-to-reorder, dark mode, then sync with Supabase so tasks follow the user to any device.

▶️ Paste the example into the code editor's HTML mode and tap Run.

Example

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
  body { font-family: system-ui, sans-serif; max-width: 420px; margin: 24px auto; padding: 0 16px; }
  form { display: flex; gap: 8px; }
  input { flex: 1; padding: 10px; border: 1px solid #ccc; border-radius: 8px; }
  button { padding: 10px 14px; border: 0; border-radius: 8px; background: #6366f1; color: white; }
  ul { list-style: none; padding: 0; }
  li { display: flex; align-items: center; gap: 8px; padding: 10px; border-bottom: 1px solid #eee; cursor: pointer; }
  li.done span { text-decoration: line-through; color: #999; }
  li .del { margin-left: auto; background: #ef4444; padding: 4px 10px; }
  .filters button { background: #e5e7eb; color: #111; }
  .filters button.active { background: #111827; color: white; }
</style>
</head>
<body>
  <h1>To-Do ✅</h1>
  <form id="form"><input id="text" placeholder="What needs doing?" required /><button>Add</button></form>
  <ul id="list"></ul>
  <div class="filters">
    <button data-f="all" class="active">All</button>
    <button data-f="active">Active</button>
    <button data-f="done">Done</button>
    <span id="left"></span>
  </div>
<script>
  let todos = JSON.parse(localStorage.getItem("todos") ?? "[]");
  let filter = "all";
  const list = document.querySelector("#list");

  const save = () => localStorage.setItem("todos", JSON.stringify(todos));

  function render() {
    list.innerHTML = "";
    todos
      .filter((t) => filter === "all" || (filter === "done" ? t.done : !t.done))
      .forEach((t) => {
        const li = document.createElement("li");
        li.dataset.id = t.id;
        li.className = t.done ? "done" : "";
        const span = document.createElement("span");
        span.textContent = t.text;                       // safe text
        const del = document.createElement("button");
        del.className = "del"; del.textContent = "✕";
        li.append(span, del);
        list.append(li);
      });
    document.querySelector("#left").textContent =
      ` ${todos.filter((t) => !t.done).length} left`;
  }

  document.querySelector("#form").addEventListener("submit", (e) => {
    e.preventDefault();
    const input = document.querySelector("#text");
    todos.push({ id: Date.now(), text: input.value.trim(), done: false });
    input.value = "";
    save(); render();
  });

  list.addEventListener("click", (e) => {                // event delegation
    const id = Number(e.target.closest("li")?.dataset.id);
    if (!id) return;
    if (e.target.matches(".del")) todos = todos.filter((t) => t.id !== id);
    else todos = todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t));
    save(); render();
  });

  document.querySelector(".filters").addEventListener("click", (e) => {
    if (!e.target.dataset.f) return;
    filter = e.target.dataset.f;
    document.querySelectorAll(".filters button").forEach((b) =>
      b.classList.toggle("active", b === e.target));
    render();
  });

  render();
</script>
</body>
</html>