codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › Live project: To-do list app

Live project: To-do list app

Build a working to-do list with JavaScript.

Combine variables, functions, events and the DOM in a small interactive app.

What you will build

Steps

  1. Read the script: update recounts the open tasks.
  2. Type a task and press Add.
  3. Click a task to cross it out.
  4. Watch the counter change.

Run it

Press Run to see the project work. Then change the data in the editor and run it again.

Challenge

Example code

<style>
  body { font-family: sans-serif; max-width: 360px; margin: 20px auto; }
  li { cursor: pointer; padding: 4px 0; }
  li.done { text-decoration: line-through; color: #888; }
</style>
<h2>My tasks (<span id="count">0</span> left)</h2>
<input id="task" placeholder="New task">
<button id="add">Add</button>
<ul id="list"></ul>
<script>
  var list = document.getElementById("list");
  var count = document.getElementById("count");
  function update() {
    count.textContent = list.querySelectorAll("li:not(.done)").length;
  }
  document.getElementById("add").onclick = function () {
    var input = document.getElementById("task");
    if (!input.value.trim()) return;
    var li = document.createElement("li");
    li.textContent = input.value.trim();
    li.onclick = function () { li.classList.toggle("done"); update(); };
    list.appendChild(li);
    input.value = "";
    update();
  };
</script>

Run this code in the online editor

← JS project: counter