To-Do List App in JavaScript
To-Do List App in JavaScript
Introduction
This simple web app allows users to add, remove, and persist tasks in their browser using
local storage.
Source Code
<!DOCTYPE html>
<html>
<head><title>To-Do List</title></head>
<body>
<h2>To-Do List</h2>
<input type="text" id="task" placeholder="Add task">
<button onclick="addTask()">Add</button>
<ul id="taskList"></ul>
<script>
function addTask() {
let task = document.getElementById("task").value;
if (task) {
let list = document.getElementById("taskList");
let item = document.createElement("li");
item.textContent = task;
item.onclick = () => {
list.removeChild(item);
saveTasks();
};
list.appendChild(item);
document.getElementById("task").value = "";
saveTasks();
}
}
function saveTasks() {
localStorage.setItem("tasks", document.getElementById("taskList").innerHTML);
}
function loadTasks() {
document.getElementById("taskList").innerHTML = localStorage.getItem("tasks");
}
loadTasks();
</script>
</body>
</html>
Explanation
This HTML+JavaScript project creates a to-do list with add/remove features. Tasks persist
across sessions via local storage, showcasing DOM manipulation and client-side storage.
Sample Output
A web interface where tasks appear as list items. Clicking on a task removes it.
Conclusion
This app demonstrates fundamental concepts of JavaScript like DOM access, event handling,
and persistence using localStorage.
How to Run
Save the file as `todo.html` and open it in any modern web browser.