AI Hints
Home›JavaScript Tutorial›To-Do List and Storage
JavaScript · Lesson 100 of 138

Practice: A To-Do List

Add, tick, delete.

⏱ 2 min read▶ Video at 1:59:32🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

The to do list is the classic first app. You type a task, add it, and click it to mark it done. We'll use the same pattern as the counter. An array of tasks is the state, and render draws the list from it.

Practice: A To-Do List
From the video: Practice: A To-Do List
  • The classic first app
  • Add and tick tasks
  • State and render again

The app

Tasks is an array of objects, our state. Render clears the list, and builds an item for each task, with the done class if it's finished. Submitting the form adds a task. Clicking an item flips its done flag. We added three tasks, then clicked the second one, so it's crossed out.

index.htmlpython
<link rel="stylesheet" href="style.css">
<form id="form"><input id="task"> <button>Add</button></form>
<ul id="list"></ul>
<script>
  const tasks = [];
  const list = document.querySelector("#list");
  function render() {
    list.replaceChildren(...tasks.map((t, i) => {
      const li = document.createElement("li");
      li.textContent = t.title;
      li.dataset.i = i;
      if (t.done) li.classList.add("done");
      return li;
    }));
  }
  form.onsubmit = (e) => { e.preventDefault();
    tasks.push({ title: task.value, done: false }); task.value = ""; render(); };
  list.onclick = (e) => { tasks[e.target.dataset.i].done ^= 1; render(); };
</script>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"type": "#task", "text": "Buy milk"}, {"key": "Enter"}, {"type": "#task", "text": "Walk the dog"}, {"key": "Enter"}, {"type": "#task", "text": "Call Sam"}, {"key": "Enter"}, {"click": "li:nth-child(2)"}]

Two shortcuts

This code uses two shortcuts to stay short. Elements with an i d are also available as global variables, like form and task. And on submit, or on click, is a quick way to set a single listener. In bigger apps, prefer query selector and add event listener, which are clearer.

keyvalue
tasks arraythe state
render()rebuild the list
onsubmit / onclickone listener each
dataset.iwhich task

Summary

The to do list keeps tasks in an array and redraws the list after every change. Next, saving the tasks, so they survive a page reload.

Key takeaways
  • Array of task objects
  • render() after changes
  • Click to toggle
Next up: localStorage — remember between visits