Table of Contents
In this project, you will build a functional To-Do List.
Start by creating an HTML file with:
<input> field with id="task".addTask().<ul> with id="list".clearAll().tasks array.<!DOCTYPE html>
<html>
<body>
<h2>To-Do List</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<input id="task" placeholder="New task">
<button onclick="addTask()">Add</button>
<ul id="list"></ul>
<button onclick="clearAll()">Clear All</button>
<script>
// Create a task Array
let tasks = [];
</script>
</body>
</html>

Result View Example
The displayTasks() function loops through the array items, wraps each item in <li> elements alongside remove buttons, and prints the result inside the list element:
// Function to display the list
function displayTasks() {
let html = "";
for (let i = 0; i < tasks.length; i++) {
html += "<li>" + tasks[i] +
" <button onclick='removeTask(" + i + ")'>x</button></li>";
}
document.getElementById("list").innerHTML = html;
}

Result View Example
Retrieves the input value, pushes it to the tasks array, resets input text, and updates list views:
// Function to Add a task
function addTask() {
let taskInput = document.getElementById("task");
let text = taskInput.value;
if (text === "") {
return;
}
tasks.push(text);
taskInput.value = "";
saveTasks();
displayTasks();
}
Removes tasks by index using the JavaScript array splice() method:
// Function to Remove a task
function removeTask(i) {
tasks.splice(i, 1);
saveTasks();
displayTasks();
}
[!NOTE]
tasks.splice(i, 1)deletes exactly 1 item at index positioni.
Empties the array and refreshes the displays:
// Function to Clear all tasks
function clearAll() {
tasks = [];
saveTasks();
displayTasks();
}
Converts the tasks array into text format and saves it inside localStorage:
// Function to Save tasks
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
[!IMPORTANT]
localStorageonly handles text keys and values. We must useJSON.stringify(tasks)to convert the tasks array into a string before saving.
Retrieves string data and parses it back into an array structure:
function loadTasks() {
let saved = localStorage.getItem("tasks");
if (saved !== null) {
tasks = JSON.parse(saved);
}
}
Combines HTML code structure and state persistence logics together:
<!DOCTYPE html>
<html>
<body>
<h2>To-Do List</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<input id="task" placeholder="New task">
<button onclick="addTask()">Add</button>
<ul id="list"></ul>
<button onclick="clearAll()">Clear All</button>
<script>
let tasks = [];
function displayTasks() {
let html = "";
for (let i = 0; i < tasks.length; i++) {
html += "<li>" + tasks[i] +
" <button onclick='removeTask(" + i + ")'>x</button></li>";
}
document.getElementById("list").innerHTML = html;
}
function addTask() {
let taskInput = document.getElementById("task");
let text = taskInput.value;
if (text === "") {
return;
}
tasks.push(text);
taskInput.value = "";
saveTasks();
displayTasks();
}
function removeTask(i) {
tasks.splice(i, 1);
saveTasks();
displayTasks();
}
function clearAll() {
tasks = [];
saveTasks();
displayTasks();
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function loadTasks() {
let saved = localStorage.getItem("tasks");
if (saved !== null) {
tasks = JSON.parse(saved);
}
}
loadTasks();
displayTasks();
</script>
</body>
</html>

Result View Example
Show an alert if the user tries to add an empty task.
Allow the user to press Enter to add a task.
Add a "Saved!" message to show when saving.
<input id="task" placeholder="New task" onkeydown="handleKey(event)">
<button onclick="addTask()">Add</button>
<ul id="list"></ul>
<button onclick="clearAll()">Clear All</button>
<p id="status" style="color: green; font-weight: bold;"></p>
<script>
// (Inside script)
function addTask() {
let taskInput = document.getElementById("task");
let text = taskInput.value.trim();
if (text === "") {
alert("Please enter a task! Task cannot be empty.");
return;
}
tasks.push(text);
taskInput.value = "";
saveTasks();
displayTasks();
}
function handleKey(event) {
if (event.key === "Enter") {
addTask();
}
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
showStatus("Saved!");
}
function showStatus(msg) {
let statusEl = document.getElementById("status");
statusEl.innerHTML = msg;
setTimeout(function () {
statusEl.innerHTML = "";
}, 2000);
}
</script>

Result View Example
Improve task workflows by adding premium styling, status checks, and filtering controls:

Result View Example
Document in project
You can Download PDF file.