bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Debugging, Projects, and Reference
JavaScript•Debugging, Projects, and Reference

Project - To-Do List

To-Do List

In this project you will build a To-Do List .

You will add tasks, remove tasks, and clear all tasks.

The tasks will be saved in an Array in localStorage .

You Will Learn

  • How to store items in an array
  • How to remove items from an array
  • How to display a list using JavaScript
  • How to save and load data using localStorage

First: Create the HTML

First create an HTML page with

  • An input field wid id="task"
  • A button for calling addTask()
  • A unordered list with id="list"
  • A button for calling clearAll()

Example

Formatted code
<!DOCTYPE html>

<html>

<body>

<h2>To-Do List</h2>

<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>

Live preview

Add a Display Function

Example

The displayTasks() function updates and displays the list.

// 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;
}

The displayTasks() function should be called whenever the list changes.

Add an Add Task Function

Example

// 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();
}

Add a Remove Task Function

Remove a task by index

// Function to Remove a task
function removeTask(i) {
 tasks.splice(i, 1);
 saveTasks();
 displayTasks();
}

The Array method splice(i, 1) removes 1 item at position i.

Add a Clear All Function

Remove everything

function removeTask(i) {
 tasks.splice(i, 1);
 saveTasks();
 displayTasks();
}

Function to Save a Task

Save in localStorage

// Function to Save tasks
function saveTasks() {
 localStorage.setItem("tasks", JSON.stringify(tasks));
}

To store arrays in localStorage, we convert them to text using JSON.stringify().

Function to Load the Tasks

Load tasks from localStorage

function loadTasks() {
 let saved = localStorage.getItem("tasks");
 if (saved !== null) {
 tasks = JSON.parse(saved);
 }
}

To load arrays, we convert them back using JSON.parse().

Final Project

Final HTML

Formatted code
<!DOCTYPE html>

<html>

<body>

<h2>To-Do List</h2>

<input id="task" placeholder="New task">

<button onclick="addTask()">Add</button>

<ul id="list"></ul>

<button onclick="clearAll()">Clear All</button>

<script>

// JavaScript goes here

</script>

</body>

</html>

Live preview

Bonus Challenges (Level Up)

Try to improve the project

  • Mark tasks as done (✔)
  • Filter tasks: All / Active / Done
  • Add "Edit task" functionality
  • Sort tasks alphabetically

JavaScript Projects

  • JavaScript Counter A counter project with restore and save in lovalStorage.
  • JavaScript Event Listener The counter project using event listener instead of onclick.
  • JavaScript Do-Do List A do-do list saved in an array in local storage.
  • JavaScript Modal Popup A modal popup window that appears on top of the page.

Previous

JavaScript Operator Precedence

Next

ECMAScript 2024

This chapter

Overview
32

Lessons

180m

Read time

1. JavaScript Errors2. JavaScript Debugging3. JavaScript Statements4. Project - localStorage Counter5. ECMAScript 20266. JavaScript Silent Errors7. JavaScript Debugging Console8. JavaScript Reserved Words9. Project - Event Listener10. ECMAScript 202511. JavaScript Error Statements12. JavaScript Debugging Breakpoints13. JavaScript Operator Precedence14. Project - To-Do List15. ECMAScript 202416. JavaScript Debugging Errors17. Project - Modal Popup18. ECMAScript 202319. JavaScript Debugging Async20. Project - Form Validation21. ECMAScript 202222. ECMAScript 202123. ECMAScript 202024. ECMAScript 201925. ECMAScript 201826. ECMAScript 201727. ECMAScript 201628. Javascript 2015 (ES6)29. JavaScript 2009 (ES5)30. JavaScript 1999 (ES3)31. JavaScript Versions32. JavaScript History

On this page

To-Do ListYou Will LearnFirst: Create the HTMLAdd a Display FunctionAdd an Add Task FunctionAdd a Remove Task FunctionAdd a Clear All FunctionFunction to Save a TaskFunction to Load the TasksFinal ProjectBonus Challenges (Level Up)JavaScript Projects