Write me a TODO app in HTML
- User
Write me a TODO app in HTML
- Assistant
Write me a TODO app in HTML
Sure! Below is a simple To-Do App built using HTML, CSS, and JavaScript. This app allows users to add new tasks, mark them as completed, and delete them. It's a great starting point, and you can further enhance it with additional features as needed.
For simplicity, we'll keep everything in a single HTML file. However, for larger projects, it's recommended to separate HTML, CSS, and JavaScript into different files.
todo-app/
│
├── index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple To-Do App</title>
<style>
/* CSS styles go here */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
margin: 0;
padding: 50px 0;
}
.todo-container {
background: #fff;
padding: 20px 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
width: 400px;
}
h1 {
text-align: center;
color: #333;
}
.input-section {
display: flex;
margin-bottom: 20px;
}
#todo-input {
flex: 1;
padding: 10px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px 0 0 4px;
outline: none;
}
#add-button {
padding: 10px 20px;
font-size: 16px;
border: none;
background: #28a745;
color: #fff;
border-radius: 0 4px 4px 0;
cursor: pointer;
transition: background 0.3s;
}
#add-button:hover {
background: #218838;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px;
background: #f9f9f9;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed .task-text {
text-decoration: line-through;
color: #888;
}
.task-text {
flex: 1;
margin-right: 10px;
}
.buttons {
display: flex;
gap: 5px;
}
.buttons button {
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
transition: opacity 0.3s;
}
.buttons button:hover {
opacity: 0.8;
}
.complete-btn {
background: #17a2b8;
color: #fff;
}
.delete-btn {
background: #dc3545;
color: #fff;
}
</style>
</head>
<body>
<div class="todo-container">
<h1>My To-Do List</h1>
<div class="input-section">
<input type="text" id="todo-input" placeholder="Enter a new task">
<button id="add-button">Add</button>
</div>
<ul id="todo-list">
<!-- To-Do items will appear here -->
</ul>
</div>
<script>
// JavaScript code goes here
document.addEventListener('DOMContentLoaded', () => {
const todoInput = document.getElementById('todo-input');
const addButton = document.getElementById('add-button');
const todoList = document.getElementById('todo-list');
// Function to add a new to-do item
function addTodo() {
const taskText = todoInput.value.trim();
if (taskText === '') {
alert('Please enter a task.');
return;
}
// Create list item elements
const li = document.createElement('li');
const span = document.createElement('span');
span.className = 'task-text';
span.textContent = taskText;
const buttonsDiv = document.createElement('div');
buttonsDiv.className = 'buttons';
const completeBtn = document.createElement('button');
completeBtn.className = 'complete-btn';
completeBtn.textContent = 'Complete';
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = 'Delete';
// Append buttons to the buttons div
buttonsDiv.appendChild(completeBtn);
buttonsDiv.appendChild(deleteBtn);
// Append span and buttons to the list item
li.appendChild(span);
li.appendChild(buttonsDiv);
// Append list item to the todo list
todoList.appendChild(li);
// Clear input field
todoInput.value = '';
todoInput.focus();
// Event listener for completing a task
completeBtn.addEventListener('click', () => {
li.classList.toggle('completed');
});
// Event listener for deleting a task
deleteBtn.addEventListener('click', () => {
todoList.removeChild(li);
});
}
// Event listener for the Add button
addButton.addEventListener('click', addTodo);
// Event listener for pressing 'Enter' key
todoInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addTodo();
}
});
});
</script>
</body>
</html>
Container (.todo-container): This div wraps the entire to-do app, including the title, input section, and the list of tasks.
Input Section:
<input>: Where users can type their new tasks.<button>: When clicked, adds the new task to the list.To-Do List (<ul id="todo-list">): An unordered list that will hold all the to-do items dynamically.
Adding Tasks:
addTodo function is triggered.<li>), and appends them to the to-do list.Completing Tasks:
.completed class, which alters the task's appearance.Deleting Tasks:
Create the File:
index.html and paste the above code into it.Open in Browser:
index.html file, or right-click and choose "Open with" followed by your preferred web browser.Start Using:
This is a basic To-Do app, but you can enhance it in various ways:
Persistence: Use localStorage to save tasks so they persist across page reloads.
// Example: Save tasks to localStorage
function saveTasks() {
const tasks = [];
todoList.querySelectorAll('li').forEach(li => {
tasks.push({
text: li.querySelector('.task-text').textContent,
completed: li.classList.contains('completed')
});
});
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// Call saveTasks() inside addTodo(), completeBtn, and deleteBtn event listeners
Editing Tasks: Allow users to edit existing tasks.
Filtering: Add options to filter tasks by all, completed, or pending.
Backend Integration: Use a backend service or database to store tasks, enabling access from multiple devices.
Styling: Improve the UI/UX with better styling, animations, and responsiveness.
MDN Web Docs:
Tutorials:
Feel free to ask if you have any questions or need further assistance!
Not yet included in a published release.