In this tutorial, we will learn how to create a simple Todo App in JavaScript.
JavaScript Todo Application
Create a folder called todo-app as project workspace and we will create all the project files inside this folder.
1. index.html
Let's create index.html and add the following code to it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ToDo App</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js" defer></script>
</head>
<body>
<h1>todos</h1>
<form id="form">
<input
type="text"
id="input"
class="input"
placeholder="Enter your todo"
autocomplete="off"
/>
<ul class="todos" id="todos"></ul>
</form>
<small
>Left click to toggle complete. <br />Right click to delete the
todo.</small
>
</body>
</html>
2. script.js
Let's create JavaScript file named script.js and add following JavaScript code to it:
const form = document.getElementById("form");
const input = document.getElementById("input");
const todosUL = document.getElementById("todos");
const todos = JSON.parse(localStorage.getItem("todos"));
if (todos) {
todos.forEach((todo) => {
addTodo(todo);
});
}
form.addEventListener("submit", (e) => {
e.preventDefault();
addTodo();
});
function addTodo(todo) {
let todoText = input.value;
if (todo) {
todoText = todo.text;
}
if (todoText) {
const todoEl = document.createElement("li");
if (todo && todo.completed) {
todoEl.classList.add("completed");
}
todoEl.innerText = todoText;
todoEl.addEventListener("click", () => {
todoEl.classList.toggle("completed");
updateLS();
});
todoEl.addEventListener("contextmenu", (e) => {
e.preventDefault();
todoEl.remove();
updateLS();
});
todosUL.appendChild(todoEl);
input.value = "";
updateLS();
}
}
function updateLS() {
const todosEl = document.querySelectorAll("li");
const todos = [];
todosEl.forEach((todoEl) => {
todos.push({
text: todoEl.innerText,
completed: todoEl.classList.contains("completed"),
});
});
localStorage.setItem("todos", JSON.stringify(todos));
}
3. style.css
Let's create CSS file named style.css and add the following CSS code to it:
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;600&display=swap");
* {
box-sizing: border-box;
}
body {
background-color: #f5f5f5;
color: #444;
display: flex;
align-items: center;
flex-direction: column;
font-family: "Poppins", sans-serif;
margin: 0;
min-height: 100vh;
}
h1 {
color: rgb(179, 131, 226);
font-size: 10rem;
text-align: center;
opacity: 0.4;
}
form {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
max-width: 100%;
width: 400px;
}
.input {
border: none;
color: #444;
font-size: 2rem;
padding: 1rem 2rem;
display: block;
width: 100%;
}
.input::placeholder {
color: #d5d5d5;
}
.todos {
background-color: #fff;
padding: 0;
margin: 0;
list-style-type: none;
}
.todos li {
border-top: 1px solid #e5e5e5;
cursor: pointer;
font-size: 1.5rem;
padding: 1rem 2rem;
}
.todos li.completed {
color: #b6b6b6;
text-decoration: line-through;
}
small {
color: #b5b5b5;
margin-top: 3rem;
text-align: center;
}
Open index.html in Browser
Let's open the index.html file in the browser and you will be able to see the following screen:
More Free JavaScript Projects with Source Code
- JavaScript Tic Tac Toe Game
- JavaScript Project - Modal Box
- JavaScript Project - Modal Box
- JavaScript Project - Password Generator
- JavaScript Project - Weather App
- JavaScript Project - Todo App
- JavaScript Project - Notes App
- JavaScript Project - Movie App
- JavaScript Project - Drawing App
- JavaScript Project - Countdown Timer
- Breakout Game with JavaScript, HTML, and CSS
- Exchange Rate Project with JavaScript, HTML, and CSS
- Expense Tracker Project with JavaScript, HTML, and CSS
- Form Validator with JavaScript, HTML, and CSS
- LyricsSearch App with JavaScript, HTML and CSS
- Movie Seat Booking Project with JavaScript, HTML, and CSS
Comments
Post a Comment