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
- An input box and an Add button
- A list where each task can be clicked to mark it done
- A live counter of tasks left
Steps
- Read the script:
updaterecounts the open tasks. - Type a task and press Add.
- Click a task to cross it out.
- 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
- Add a Delete button to every task.
- Save the tasks with
localStorage.
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>