Final project: a to-do app in the browser
The time has come to bring together everything you have learned in a real project: a to-do application that works in the browser and remembers your data even if you close the page. You will practice variables, functions, arrays and their methods, objects, JSON, the DOM, events, forms and localStorage. You need nothing installed except a text editor and a browser, although it is wise to serve the folder with npx serve in order to use modules.
Requirements
- Add a task by typing a text in a form (empty texts are not accepted).
- Mark a task as done or pending with a click and delete it with a button.
- Filter the list: all, pending and done.
- Show a counter of pending tasks.
- Save and load tasks with localStorage, protected against corrupted data.
- Use textContent (never innerHTML) to show the user's text.
- Code split into small functions and const by default.
Step 1: the HTML structure
Create a folder with index.html, style.css (optional) and app.js. The HTML contains a form with a field and a button, three filter buttons with a data-filter attribute, the empty list and a paragraph for the counter. This way you separate structure (HTML) from behavior (JavaScript).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>To-do app</title>
<style>
body { font-family: sans-serif; max-width: 480px; margin: 2rem auto; }
li { cursor: pointer; padding: 0.3rem 0; }
li.done span { text-decoration: line-through; color: gray; }
button.active { font-weight: bold; }
</style>
</head>
<body>
<h1>My tasks</h1>
<form id="task-form">
<input id="task-input" type="text" placeholder="New task" autocomplete="off" />
<button type="submit">Add</button>
</form>
<div id="filters">
<button data-filter="all" class="active">All</button>
<button data-filter="pending">Pending</button>
<button data-filter="done">Done</button>
</div>
<ul id="task-list"></ul>
<p id="counter"></p>
<script type="module" src="app.js"></script>
</body>
</html>Step 2: state, storage and rendering
The central idea is that the application state lives in an array of objects, and the screen is just a reflection of that state. Every time something changes, you modify the array, save it and call a render function that redraws the list. This pattern is the base of frameworks like React, and it prevents mismatches between what you see and what is in memory. Each task is an object with id, text and done.
// app.js
const STORAGE_KEY = 'todo-tasks';
const form = document.querySelector('#task-form');
const input = document.querySelector('#task-input');
const list = document.querySelector('#task-list');
const counter = document.querySelector('#counter');
const filters = document.querySelector('#filters');
function loadTasks() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
const data = raw ? JSON.parse(raw) : [];
return Array.isArray(data) ? data : [];
} catch {
return [];
}
}
function saveTasks() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(tasks));
}
let tasks = loadTasks();
let currentFilter = 'all';
function visibleTasks() {
if (currentFilter === 'pending') return tasks.filter((t) => !t.done);
if (currentFilter === 'done') return tasks.filter((t) => t.done);
return tasks;
}
function render() {
list.textContent = ''; // clear the list
for (const task of visibleTasks()) {
const li = document.createElement('li');
li.dataset.id = task.id;
li.classList.toggle('done', task.done);
const label = document.createElement('span');
label.textContent = task.text; // safe: plain text
const remove = document.createElement('button');
remove.textContent = 'x';
remove.dataset.action = 'delete';
li.append(label, ' ', remove);
list.append(li);
}
const pending = tasks.filter((t) => !t.done).length;
counter.textContent = `${pending} pending task(s)`;
}Step 3: events
Connect three events. The form's submit adds a new task; the click on the list, using delegation, toggles the state or deletes depending on where it is pressed; the click on the filters changes currentFilter. After each data change call saveTasks and render. Use Date.now() as a simple unique id, and filter to delete without mutating the array.
form.addEventListener('submit', (event) => {
event.preventDefault();
const text = input.value.trim();
if (text === '') return;
tasks = [...tasks, { id: Date.now(), text, done: false }];
input.value = '';
input.focus();
saveTasks();
render();
});
list.addEventListener('click', (event) => {
const li = event.target.closest('li');
if (!li) return;
const id = Number(li.dataset.id);
if (event.target.dataset.action === 'delete') {
tasks = tasks.filter((t) => t.id !== id);
} else {
tasks = tasks.map((t) => (t.id === id ? { ...t, done: !t.done } : t));
}
saveTasks();
render();
});
filters.addEventListener('click', (event) => {
const button = event.target.closest('button[data-filter]');
if (!button) return;
currentFilter = button.dataset.filter;
for (const b of filters.querySelectorAll('button')) {
b.classList.toggle('active', b === button);
}
render();
});
render();Completion criteria and improvements
Your project is finished when: you can add, mark, delete and filter tasks; the counter is always correct; after reloading the page the tasks are still there; an empty text does not create tasks; and there are no errors in the console. If you have energy left, try these improvements: editing the text with a double click, a button to delete all completed tasks, a due-date field, sorting with sort and loading sample tasks from an API with fetch and async/await. Each one will make you practice something different from what you learned in these eight weeks. Congratulations: you now know how to program in JavaScript.
जो सीखा उसे परखें
प्रश्न 1 / 7What does a function marked async always return?
✎ मेरे नोट्स
केवल इस ब्राउज़र में सहेजा गया। खाते में रखने के लिए साइन इन करें।0/5000 अक्षर