← Learning Hub
Advanced

Project: To-do App with localStorage

Let's build a real little app: add tasks, tick them off, delete them, and keep them even after the page is closed. It uses the DOM, events, arrays, objects, JSON and localStorage.

A to-do app looks simple, but it contains the core pattern of almost every web app: keep data in an array, draw the page from that data, change the data when the user acts, redraw, and save. Once you understand this project, React and Angular will make much more sense - they automate the "redraw" part.

localStorage in one minute

localStorage saves small text values in the browser, for this website only. It stays after a refresh. Store objects by turning them into JSON.

Example
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme"));   // dark
localStorage.setItem("todos", JSON.stringify([{ text: "Learn JS", done: false }]));
const todos = JSON.parse(localStorage.getItem("todos") || "[]");

The full app

Example
<!DOCTYPE html>
<html>
  <head>
    <style>
      body { font-family: system-ui, sans-serif; max-width: 420px; margin: 30px auto; padding: 0 12px; }
      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: #7c3aed; color: #fff; cursor: pointer; }
      li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid #eee; list-style: none; }
      li.done span { text-decoration: line-through; color: #999; }
      li span { flex: 1; }
      .del { background: #ef4444; padding: 4px 10px; }
      ul { padding: 0; }
    </style>
  </head>
  <body>
    <h2>My tasks</h2>
    <form id="form">
      <input id="task" placeholder="What do you need to do?" required>
      <button>Add</button>
    </form>
    <ul id="list"></ul>
    <p id="count"></p>

    <script>
      const KEY = "terixto-todos";
      let todos = load();

      function load() {
        try { return JSON.parse(localStorage.getItem(KEY)) || []; }
        catch { return []; }
      }
      function save() {
        try { localStorage.setItem(KEY, JSON.stringify(todos)); } catch {}
      }

      function render() {
        const list = document.getElementById("list");
        list.innerHTML = "";
        todos.forEach((t, i) => {
          const li = document.createElement("li");
          li.className = t.done ? "done" : "";

          const box = document.createElement("input");
          box.type = "checkbox";
          box.checked = t.done;
          box.addEventListener("change", () => { t.done = box.checked; save(); render(); });

          const text = document.createElement("span");
          text.textContent = t.text;           // textContent is safe: no HTML is run

          const del = document.createElement("button");
          del.textContent = "Delete";
          del.className = "del";
          del.addEventListener("click", () => { todos.splice(i, 1); save(); render(); });

          li.append(box, text, del);
          list.append(li);
        });
        const left = todos.filter(t => !t.done).length;
        document.getElementById("count").textContent = `${left} of ${todos.length} left`;
      }

      document.getElementById("form").addEventListener("submit", e => {
        e.preventDefault();                    // stop the page from reloading
        const input = document.getElementById("task");
        todos.push({ text: input.value.trim(), done: false });
        input.value = "";
        save();
        render();
      });

      render();
    </script>
  </body>
</html>
▶ Try it yourself

Challenges

  • Add a "Clear finished" button using filter.
  • Add All / Active / Done filter buttons.
  • Let the user edit a task by double-clicking it.
  • Save the date each task was added and show it.
In the Try it editor, your tasks are kept while the editor page is open (press Run - they stay). Save the code as an .html file and open it in your browser to keep them for good.
Notice textContent instead of innerHTML for the user's text. If you put user text into innerHTML, someone could inject HTML or scripts into your page.

📝 Test yourself

1. What does e.preventDefault() do in a form's submit event?
2. How do you store an array in localStorage?
3. Why use textContent for the user's task text?