console
<!DOCTYPE html>
<html>
<head>
<title>Todo List</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
margin-top: 0;
}
.add-todo {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.add-todo input[type=text] {
flex-grow: 1;
margin-right: 10px;
padding: 10px;
border-radius: 3px;
border: none;
font-size: 16px;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
}
.add-todo button {
background-color: #3C3C3C;
color: #fff;
border: none;
padding: 10px;
cursor: pointer;
border-radius: 3px;
font-size: 16px;
transition: background-color 0.2s ease-in-out;
}
.add-todo button:hover {
background-color: #444;
}
.category-btns {
display: flex;
margin-bottom: 20px;
}
.category-btn {
background-color: #3C3C3C;
color: #fff;
border: none;
padding: 10px;
cursor: pointer;
border-radius: 3px;
font-size: 16px;
transition: background-color 0.2s ease-in-out;
margin-right: 10px;
}
.category-btn.active {
background-color: #444;
}
.todo-card {
background-color: #fff;
box-shadow: 0px 0px 5px rgba(0, 0, 0
, 0.1);
border-radius: 5px;
padding: 20px;
margin-bottom: 20px;
}
.todo-card.completed {
text-decoration: line-through;
color: #aaa;
}
.todo-card .title {
font-weight: bold;
margin-bottom: 10px;
}
.todo-card .category {
font-size: 12px;
margin-bottom: 10px;
color: #888;
}
.category-work .todo-card {
border-left: 5px solid #1abc9c;
}
.category-study .todo-card {
border-left: 5px solid #3498db;
}
.category-shopping .todo-card {
border-left: 5px solid #f1c40f;
}
@media screen and (max-width: 600px) {
.container {
padding: 10px;
}
.add-todo {
flex-wrap: wrap;
margin-bottom: 10px;
}
.add-todo input[type=text] {
width: 100%;
margin-right: 0;
margin-bottom: 10px;
}
.category-btns {
flex-wrap: wrap;
margin-bottom: 10px;
}
.category-btn {
margin-right: 10px;
margin-bottom: 10px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Todo List</h1>
<div class="add-todo">
<input type="text" id="todo-input" placeholder="Add a new todo">
<button id="add-btn">Add</button>
</div>
<div class="category-btns">
<button class="category-btn active" data-category="all">All</button>
<button class="category-btn" data-category="work">Work</button>
<button class="category-btn" data-category="study">Study</button>
<button class="category-btn" data-category="shopping">Shopping</button>
</div>
<div class="todo-list">
</div>
</div>
<script>
const todoInput = document.getElementById("todo-input");
const addBtn = document.getElementById("add-btn");
const todoList = document.querySelector(".todo-list");
const categoryBtns = document.querySelectorAll(".category-btn");
let todos = [];
function addTodo() {
const todoText = todoInput.value.trim();
if (!todoText) {
return;
}
const todo = {
id: Date.now(),
text: todoText,
completed: false,
category: "all"
};
todos.push(todo);
renderTodos();
todoInput.value = "";
}
function deleteTodoById(id) {
todos = todos.filter(todo => todo.id !== id);
renderTodos();
}
function toggleTodoComplete(id) {
const todo = todos.find(todo => todo.id === id);
todo.completed = !todo.completed;
renderTodos();
}
function renderTodos(category = "all") {
const filteredTodos = todos.filter(todo => category === "all" || todo.category === category);
todoList.innerHTML = "";
filteredTodos.forEach(todo => {
const todoCard = document.createElement("div");
todoCard.classList.add("todo-card");
todoCard.classList.toggle("completed", todo.completed);
const title = document.createElement("div");
title.classList.add("title");
title.textContent = todo.text;
const category = document.createElement("div");
category.classList.add("category");
category.textContent = todo.category;
const deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.addEventListener("click", () => deleteTodoById(todo.id));
const completeBtn = document.createElement("button");
completeBtn.textContent = todo.completed ? "Undo" : "Complete";
completeBtn.addEventListener("click", () => toggleTodoComplete(todo.id));
todoCard.appendChild(title);
todoCard.appendChild(category);
todoCard.appendChild(deleteBtn);
todoCard.appendChild(completeBtn);
todoList.appendChild(todoCard);
});
}
addBtn.addEventListener("click", addTodo);
categoryBtns.forEach(btn => {
btn.addEventListener("click", () => {
categoryBtns.forEach(btn => btn.classList.remove("active"));
btn.classList.add("active");
const category = btn.dataset.category;
renderTodos(category);
});
});
</script>
</body>