Introduction
Creating a todo app in JS is easy as we write the code. Before starting without a project we must have a basic understanding of HTML, CSS, and JavaScript.
Prerequisites
Basic knowledge of HTML
Basic knowledge of CSS
Basic knowledge of JS
Creating our App
Let's understand the app we are going to build. It will be a simple to-do app where users can add, edit, and delete items.
First, create a directory for the todo app. Create a file inside for HTML, CSS, and JavaScript.
mkdir todo && cd todo
touch app.js && touch styles.css && touch index.html
Add the following code to index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/meistericons@latest/fonts/mni.css"
/>
<link rel="stylesheet" href="./styles.css" />
<title>Todo App - HTML, CSS and JS</title>
</head>
<body>
<main class="container">
<h1>Todos</h1>
<form class="">
<input
type="text"
name="todo"
id="todo"
placeholder="+ Add New Todo"
name="Take a walk"
/>
<button type="submit">Add</button>
</form>
<div class="tab_container">
<span class="active_tab tab">Remaning Todos</span>
<span class="tab">Completed Todos</span>
</div>
<strong class="active__todo">Remaning Todos</strong>
<ul class="todo__container"></ul>
</main>
<script src="./app.js"></script>
</body>
</html>
Add the following code to styles.css
body,
html,
:root,
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 20rem;
height: 30rem;
border-radius: 2rem;
border: 1px solid rgb(34, 36, 192);
padding: 2rem;
position: fixed;
top: 10rem;
left: 38%;
}
h1 {
font-size: 3rem;
font-weight: 700;
margin-bottom: 1rem;
}
div {
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
margin: 1rem 0;
}
.todo__active {
border-bottom: 4px solid rgb(34, 36, 192);
}
.active__todo {
font-size: 1.25rem;
font-weight: 600;
}
ul {
margin-top: 1rem;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
li {
list-style-type: none;
display: flex;
gap: 0.25rem;
}
label {
font-size: 1rem;
font-weight: 500;
}
.tab_container {
display: flex;
gap: 1rem;
}
.tab {
font-weight: 600;
font-size: 0.85rem;
}
.tab:hover {
cursor: pointer;
}
.active_tab {
border-bottom: 4px solid rgb(34, 36, 192);
font-weight: 700;
font-size: 1rem;
}
.todo__container {
height: 15rem;
overflow-y: scroll;
}
.todo {
display: flex;
align-items: center;
justify-content: space-between;
}
.todo__item__span {
display: flex;
align-items: center;
gap: 0.5rem;
}
Add the following code to the app.js file.
// List of Tasks
let todos = [
{ title: "Example 1", completed: false },
{ title: "Example 2", completed: false },
{ title: "Example 3", completed: false },
];
const todoContainer = document.querySelector(".todo__container");
const remainingTodoTab = document.querySelectorAll(".tab")[0];
const completedTodoTab = document.querySelectorAll(".tab")[1];
const todoHeader = document.querySelector(".active__todo");
// RemainingTasks event listerner.
remainingTodoTab.addEventListener("click", () => {
completedTodoTab.classList.remove("active_tab");
remainingTodoTab.classList.add("active_tab");
todoHeader.innerHTML = "Remaining Todos";
clearTodoList();
todos
.filter(({ completed }) => !completed)
.forEach((remainingTodo) => {
createTodoItem(remainingTodo.title);
});
});
// CompletedTasks event listener.
completedTodoTab.addEventListener("click", () => {
remainingTodoTab.classList.remove("active_tab");
completedTodoTab.classList.add("active_tab");
todoHeader.innerHTML = "Completed Todos";
clearTodoList();
todos
.filter(({ completed }) => completed === true)
.forEach((completedTodo) => {
createTodoItem(completedTodo.title, true);
});
});
// Clear Task List
const clearTodoList = () => {
todoContainer.innerHTML = "";
};
/**
* Remove task from tasks list.
* @param {*} removeTodo - String to remove from tasks list.
*/
const removeTodo = (removeTodo) => {
todos = todos.filter((todo) => todo.title !== removeTodo);
};
/**
*
* @param {*} task - String to add to tasks list.
* @param {*} isCompleted - Status of the task.
*/
const createTodoItem = (task, isCompleted = false) => {
const newListItem = document.createElement("li");
newListItem.classList.add("todo");
const newTodoSpan = document.createElement("span");
newTodoSpan.classList.add("todo__item__span");
const newTodo = document.createElement("label");
const input = document.createElement("input");
const deleteIcon = document.createElement("i");
deleteIcon.classList.add("mni-delete-aB");
// Delete Task Click Event Listner.
deleteIcon.addEventListener("click", () => {
removeTodo(task);
newListItem.remove();
});
if (isCompleted) {
input.checked = true;
newListItem.style.textDecoration = "line-through";
} else {
input.checked = false;
}
input.type = "checkbox";
/**
* Update checkbox status.
* @param {*} e - Events in input element.
*/
input.onchange = (e) => {
const currentTodo = todos.find((todo) => todo.title === e.target.name);
if (e.target.checked) {
e.target.parentElement.style.textDecoration = "line-through";
currentTodo.completed = true;
} else {
currentTodo.completed = false;
e.target.parentElement.style.textDecoration = "none";
}
newListItem.remove();
};
input.name = task;
newListItem.appendChild(newTodoSpan);
newTodoSpan.appendChild(input);
newTodoSpan.appendChild(newTodo);
newListItem.appendChild(deleteIcon);
newTodo.innerHTML = task;
todoContainer.appendChild(newListItem);
if (!todos.length) {
todos.push({ title: task, completed: false });
}
};
// Create a list of tasks
todos.forEach((todo) => {
createTodoItem(todo.title);
});
// Add task to the list.
const addTodo = (e) => {
e.preventDefault();
const todo = document.getElementById("todo").value;
if (todo.length > 0 && remainingTodoTab.classList.contains("active_tab")) {
todos.push({ title: todo, completed: false });
createTodoItem(todo);
}
document.getElementById("todo").value = "";
};
document.querySelector("form").addEventListener("submit", addTodo);
Let's understand each of the lines of code.
Initially, a list of example tasks is added.
let todos = [
{ title: "Example 1", completed: false },
{ title: "Example 2", completed: false },
{ title: "Example 3", completed: false },
];
We will add an event listener for the form submission and add a function for it.
const addTodo = (e) => {
e.preventDefault();
const todo = document.getElementById("todo").value;
if (todo.length > 0 && remainingTodoTab.classList.contains("active_tab")) {
todos.push({ title: todo, completed: false });
createTodoItem(todo);
}
document.getElementById("todo").value = "";
};
document.querySelector("form").addEventListener("submit", addTodo);
We also need to create a function to append a new task list item after the user submits the form.
const createTodoItem = (task, isCompleted = false) => {};
First, we create the tags necessary for the list item to check if the selected task is completed.
if (isCompleted) {
input.checked = true;
newListItem.style.textDecoration = "line-through";
} else {
input.checked = false;
}
input.type = "checkbox";
Add an event handler for the input to update the completed and remaining tasks.
/**
* Update checkbox status.
* @param {*} e - Events in input element.
*/
input.onchange = (e) => {
const currentTodo = todos.find((todo) => todo.title === e.target.name);
if (e.target.checked) {
e.target.parentElement.style.textDecoration = "line-through";
currentTodo.completed = true;
} else {
currentTodo.completed = false;
e.target.parentElement.style.textDecoration = "none";
}
newListItem.remove();
};
input.name = task;
Finally, append all the created tags to the html body
newListItem.appendChild(newTodoSpan);
newTodoSpan.appendChild(input);
newTodoSpan.appendChild(newTodo);
newListItem.appendChild(deleteIcon);
newTodo.innerHTML = task;
todoContainer.appendChild(newListItem);
if (!todos.length) {
todos.push({ title: task, completed: false });
}
Now, let us add an event listener to our delete icon.
// Delete Task Click Event Listner.
deleteIcon.addEventListener("click", () => {
removeTodo(task);
newListItem.remove();
});
and the function to remove the task.
// Clear Task List
const clearTodoList = () => {
todoContainer.innerHTML = "";
};
/**
* Remove task from tasks list.
* @param {*} removeTodo - String to remove from tasks list.
*/
const removeTodo = (removeTodo) => {
todos = todos.filter((todo) => todo.title !== removeTodo);
};
Conclusion
We have built a complete to-do app where users can add tasks. View Remaining and completed tasks. Update tasks to be completed and/or uncompleted. We have done it all using our knowledge of HTML, CSS, and javascript. You can now easily update the UI to your liking and extra features to the app to make it even better.