Lesson 7.5 β To-Do List Project
π Building a Task Management Application Using HTML, CSS & JavaScript
π― Learning Objectives
After completing this lesson, students will be able to:
β Build a To-Do List Application
β Add Tasks Dynamically
β Delete Tasks
β Use Arrays
β Use DOM Manipulation
β Handle User Events
β Build Real-World Productivity Applications
π Introduction
Every day we have tasks to complete.
Examples:
β Complete Assignment
β Attend Class
β Practice Coding
β Submit Project
Managing tasks manually can be difficult.
Modern applications like:
β Google Tasks
β Microsoft To Do
β Notion
β Trello
help users manage tasks.
Today we will build:
β To-Do List Application
π Project Preview
------------------------
TO-DO LIST
[ Enter Task ]
[ Add Task ]
------------------------
β Complete Homework
β Learn JavaScript
β Practice Coding
β Delete
------------------------
π― Concepts Used
| Concept | Used |
|---|---|
| Variables | β |
| Arrays | β |
| Functions | β |
| Events | β |
| DOM Manipulation | β |
| Loops | β |
ποΈ Project Files
Create:
index.html
style.css
script.js
π― Step 1 β Create HTML Structure
index.html
<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text"
id="taskInput"
placeholder="Enter Task">
<button onclick="addTask()">
Add Task
</button>
<ul id="taskList">
</ul>
</div>
<script src="script.js"></script>
</body>
</html>
π Understanding HTML
Input Box:
<input type="text" id="taskInput">
Used to enter tasks.
Button:
<button onclick="addTask()">
Adds a task.
Task Area:
<ul id="taskList">
Displays all tasks.
π― Step 2 β Design the Project
style.css
body{
font-family:Arial;
background:#f4f4f4;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
.container{
background:white;
padding:30px;
width:400px;
border-radius:10px;
box-shadow:0 0 15px lightgray;
text-align:center;
}
input{
padding:10px;
width:250px;
font-size:16px;
}
button{
padding:10px 15px;
background:#0d6efd;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
}
button:hover{
background:#084298;
}
ul{
list-style:none;
padding:0;
}
li{
padding:10px;
margin-top:10px;
background:#f1f1f1;
border-radius:5px;
display:flex;
justify-content:space-between;
align-items:center;
}
π― Step 3 β Create Task Array
script.js
let tasks = [];
π Why Use an Array?
Array stores all tasks.
Example:
tasks =
[
"Homework",
"Coding",
"Project"
];
π― Step 4 β Create Add Task Function
function addTask()
{
}
π Get User Input
let task =
document.getElementById("taskInput").value;
Example:
Complete Homework
π― Validate Input
if(task == "")
{
alert("Please Enter Task");
return;
}
π Add Task to Array
tasks.push(task);
Example:
Before:
Homework
Coding
After:
Homework
Coding
Project
π― Display Tasks
Create function:
function displayTasks()
{
}
π Get Task List Area
let taskList =
document.getElementById("taskList");
π Clear Existing List
taskList.innerHTML = "";
Prevents duplicate display.
π― Loop Through Tasks
for(let i = 0; i < tasks.length; i++)
{
}
Access each task.
π Create List Item
let li =
document.createElement("li");
Set Task Text
li.innerHTML = tasks[i];
Add To List
taskList.appendChild(li);
π― Complete Display Function
function displayTasks()
{
let taskList =
document.getElementById("taskList");
taskList.innerHTML = "";
for(let i = 0; i < tasks.length; i++)
{
let li =
document.createElement("li");
li.innerHTML = tasks[i];
taskList.appendChild(li);
}
}
π Update addTask()
function addTask()
{
let task =
document.getElementById("taskInput").value;
if(task == "")
{
alert("Please Enter Task");
return;
}
tasks.push(task);
displayTasks();
document.getElementById("taskInput").value = "";
}
π― Add Delete Button
Create button dynamically.
let deleteButton =
document.createElement("button");
Set Text
deleteButton.innerHTML = "Delete";
π Delete Function
deleteButton.onclick = function()
{
deleteTask(i);
};
π― Create deleteTask()
function deleteTask(index)
{
tasks.splice(index,1);
displayTasks();
}
π What is splice()?
Removes array items.
Example:
tasks.splice(1,1);
Before:
Homework
Coding
Project
After:
Homework
Project
π― Complete Display Function
function displayTasks()
{
let taskList =
document.getElementById("taskList");
taskList.innerHTML = "";
for(let i = 0; i < tasks.length; i++)
{
let li =
document.createElement("li");
li.innerHTML = tasks[i];
let deleteButton =
document.createElement("button");
deleteButton.innerHTML = "Delete";
deleteButton.onclick = function()
{
deleteTask(i);
};
li.appendChild(deleteButton);
taskList.appendChild(li);
}
}
π» Complete Project Code
script.js
let tasks = [];
function addTask()
{
let task =
document.getElementById("taskInput").value;
if(task == "")
{
alert("Please Enter Task");
return;
}
tasks.push(task);
displayTasks();
document.getElementById("taskInput").value = "";
}
function displayTasks()
{
let taskList =
document.getElementById("taskList");
taskList.innerHTML = "";
for(let i = 0; i < tasks.length; i++)
{
let li =
document.createElement("li");
li.innerHTML = tasks[i];
let deleteButton =
document.createElement("button");
deleteButton.innerHTML = "Delete";
deleteButton.onclick = function()
{
deleteTask(i);
};
li.appendChild(deleteButton);
taskList.appendChild(li);
}
}
function deleteTask(index)
{
tasks.splice(index,1);
displayTasks();
}
π― Expected Output
User Enters:
Learn JavaScript
Clicks:
Add Task
Output:
Learn JavaScript Delete
User Clicks:
Delete
Task disappears.
π Project Improvements
Add:
β Task Completion Checkbox
β Edit Task Button
β Task Counter
β Save Tasks in Browser
β Dark Mode
π« Real World Applications
To-Do Lists are used in:
β Task Managers
β Project Management Tools
β Student Portals
β Office Applications
β Productivity Apps
β Mobile Applications
β οΈ Common Beginner Mistakes
Mistake 1
Forgetting displayTasks()
Task added but not shown.
Mistake 2
Wrong ID Name
taskInput
must match HTML.
Mistake 3
Forgetting splice()
Delete button will not work.
π Best Practices
β Validate Input
β Use Arrays Properly
β Keep Functions Small
β Separate HTML, CSS, and JavaScript
β Test Add/Delete Features
π Project Challenge
Modify the project and add:
β Complete Task Checkbox
β Edit Task Button
β Task Counter
β Current Date
β Local Storage Support
π Practical Activity
Create:
Student Daily Planner
Features:
- Add Task
- Delete Task
- Mark Complete
- Task Counter
π Assignment
Assignment 7.5
- What is a To-Do List?
- Why is an Array used?
- What does
push()do? - What does
splice()do? - Create a To-Do List project.
- Add a Delete Button.
- Add a Task Counter.
π§ Quiz
Q1. Which data structure stores tasks?
A. Function
B. Array
C. Event
D. Loop
β Answer: B
Q2. Which method adds an item to an array?
A. add()
B. push()
C. insert()
D. create()
β Answer: B
Q3. Which method removes an item from an array?
A. delete()
B. pop()
C. splice()
D. remove()
β Answer: C
Q4. To-Do List mainly uses?
A. DOM Manipulation
B. Arrays
C. Functions
D. All of These
β Answer: D
Q5. Which button removes a task?
A. Add
B. Edit
C. Delete
D. Save
β Answer: C
π Lesson Summary
β Built a complete To-Do List application.
β Learned how to store tasks in arrays.
β Used push() and splice().
β Applied DOM Manipulation.
β Created a real-world productivity application.