Project: Interactive To-Do List
Bring it all together — variables, arrays, the DOM and events — to build a real, working to-do app.
What you will learn
- Combine DOM, events and arrays
- Add and remove items dynamically
- Build a complete interactive feature
The brief
Build a to-do list where you can type a task, add it to the list, and remove it. No frameworks — just the JavaScript you now know.
Everything here is something you have already met: getElementById to grab elements, addEventListener for clicks, createElement/appendChild to build the page, and .remove() to delete. Read the code, then we will walk the whole flow.
<input id="task" placeholder="What needs doing?">
<button id="add">Add</button>
<ul id="list"></ul>
<script>
const input = document.getElementById("task");
const list = document.getElementById("list");
document.getElementById("add").addEventListener("click", function() {
const text = input.value.trim();
if (text === "") return;
const li = document.createElement("li");
li.textContent = text;
const del = document.createElement("button");
del.textContent = " ✕";
del.addEventListener("click", () => li.remove());
li.appendChild(del);
list.appendChild(li);
input.value = "";
});
</script>Here is the full add-and-delete flow, step by step:
- We grab the text box (
input) and the empty list (list) once at the top so we can reuse them. - When Add is clicked,
input.value.trim()reads what was typed and trims spaces. if (text === "") returnguards against empty tasks — if the box is blank, we stop early and add nothing.createElement("li")makes a new list item andli.textContent = textputs the task words in it.- We also build a small
✕button, and give it its own click handler:() => li.remove()deletes that exact item. li.appendChild(del)puts the ✕ inside the item, thenlist.appendChild(li)adds the finished item to the page.- Finally
input.value = ""clears the box, ready for the next task.
Note: Output: Type "Buy milk" and click Add → a row "Buy milk ✕" appears. Type "Call Asha" and click Add → a second row appears below it. Click the ✕ on a row → just that row disappears.
Tip: Enhance it: mark tasks done on click (toggle a line-through style), save tasks in localStorage so they survive a refresh, or show a count of remaining tasks.
✍️ Practice
- Build the to-do app and confirm you can add and delete tasks.
- Add a “mark done” feature that toggles a line-through style.
🏠 Homework
- Save the to-do list in
localStorageso the tasks remain after a page refresh.