I'll create a Kanban-style board with three columns for better organization.
Let me build this step by step. # To-Do List Application with Drag-and-Drop
Here's a comprehensive to-do list application with Kanban-style columns, drag-and-drop functionality, and local storage persistence.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TaskFlow - To-Do List</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--primary: #6366f1;
--primary-dark: #4f46e5;
--todo-color: #f59e0b;
--progress-color: #3b82f6;
--done-color: #10b981;
--bg-gradient: linear-gradient(135deg, #1e1b4b 0%, #312e81 50%, #1e1b4b 100%);
--card-bg: rgba(255, 255, 255, 0.95);
--column-bg: rgba(255, 255, 255, 0.1);
--text-dark: #1f2937;
--text-light: #6b7280;
--shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: var(--bg-gradient);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1400px;
margin: 0 auto;
}
/* Header Styles */
.header {
text-align: center;
margin-bottom: 30px;
color: white;
}
.header h1 {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 10px;
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}
.header h1 span {
color: #a5b4fc;
}
.header p {
color: #c7d2fe;
font-size: 1.1rem;
}
/* Stats Bar */
.stats-bar {
display: flex;
justify-content: center;
gap: 30px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.stat-item {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
padding: 15px 25px;
border-radius: 15px;
text-align: center;
color: white;
min-width: 120px;
}
.stat-item .number {
font-size: 2rem;
font-weight: 700;
}
.stat-item .label {
font-size: 0.85rem;
color: #c7d2fe;
text-transform: uppercase;
letter-spacing: 1px;
}
/* Add Task Section */
.add-task-section {
background: var(--card-bg);
border-radius: 20px;
padding: 25px;
margin-bottom: 30px;
box-shadow: var(--shadow);
}
.add-task-form {
display: flex;
gap: 15px;
flex-wrap: wrap;
}
.input-group {
flex: 1;
min-width: 200px;
}
.input-group label {
display: block;
font-size: 0.85rem;
font-weight: 600;
color: var(--text-light);
margin-bottom: 5px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.input-group input,
.input-group select,
.input-group textarea {
width: 100%;
padding: 12px 15px;
border: 2px solid #e5e7eb;
border-radius: 10px;
font-size: 1rem;
transition: all 0.3s ease;
font-family: inherit;
}
.input-group input:focus,
.input-group select:focus,
.input-group textarea:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}
.input-group textarea {
resize: vertical;
min-height: 80px;
}
.priority-select {
position: relative;
}
.add-btn {
padding: 12px 30px;
background: var(--primary);
color: white;
border: none;
border-radius: 10px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
align-self: flex-end;
display: flex;
align-items: center;
gap: 8px;
}
.add-btn:hover {
background: var(--primary-dark);
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(99, 102, 241, 0.4);
}
/* Board Styles */
.board {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 25px;
}
.column {
background: var(--column-bg);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 20px;
min-height: 400px;
}
.column-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 2px solid rgba(255, 255, 255, 0.1);
}
.column-title {
display: flex;
align-items: center;
gap: 10px;
color: white;
font-size: 1.1rem;
font-weight: 600;
}
.column-icon {
width: 35px;
height: 35px;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
}
.column.todo .column-icon {
background: rgba(245, 158, 11, 0.2);
}
.column.progress .column-icon {
background: rgba(59, 130, 246, 0.2);
}
.column.done .column-icon {
background: rgba(16, 185, 129, 0.2);
}
.task-count {
background: rgba(255, 255, 255, 0.2);
color: white;
padding: 5px 12px;
border-radius: 20px;
font-size: 0.85rem;
font-weight: 600;
}
/* Task List */
.task-list {
min-height: 300px;
transition: all 0.3s ease;
}
.task-list.drag-over {
background: rgba(255, 255, 255, 0.1);
border-radius: 15px;
}
/* Task Card */
.task-card {
background: var(--card-bg);
border-radius: 15px;
padding: 18px;
margin-bottom: 15px;
cursor: grab;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.task-card::before {
content: '';
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 4px;
}
.task-card.priority-high::before {
background: #ef4444;
}
.task-card.priority-medium::before {
background: #f59e0b;
}
.task-card.priority-low::before {
background: #10b981;
}
.task-card:hover {
transform: translateY(-3px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.task-card.dragging {
opacity: 0.5;
transform: rotate(3deg);
cursor: grabbing;
}
.task-card.drag-preview {
opacity: 0.8;
transform: scale(1.02);
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
}
.task-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 10px;
}
.task-title {
font-weight: 600;
color: var(--text-dark);
font-size: 1rem;
word-break: break-word;
flex: 1;
margin-right: 10px;
}
.task-title.completed {
text-decoration: line-through;
color: var(--text-light);
}
.task-actions {
display: flex;
gap: 5px;
opacity: 0;
transition: opacity 0.3s ease;
}
.task-card:hover .task-actions {
opacity: 1;
}
.task-action-btn {
width: 30px;
height: 30px;
border: none;
border-radius: 8px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.9rem;
transition: all 0.2s ease;
}
.edit-btn {
background: #e0e7ff;
color: #4f46e5;
}
.edit-btn:hover {
background: #c7d2fe;
}
.delete-btn {
background: #fee2e2;
color: #dc2626;
}
.delete-btn:hover {
background: #fecaca;
}
.task-description {
color: var(--text-light);
font-size: 0.9rem;
margin-bottom: 12px;
line-height: 1.5;
}
.task-footer {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.task-meta {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.task-tag {
padding: 4px 10px;
border-radius: 20px;
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.priority-high {
background: #fee2e2;
color: #dc2626;
}
.priority-medium {
background: #fef3c7;
color: #d97706;
}
.priority-low {
background: #d1fae5;
color: #059669;
}
.task-date {
color: var(--text-light);
font-size: 0.8rem;
display: flex;
align-items: center;
gap: 5px;
}
/* Checkbox */
.task-checkbox {
position: relative;
margin-right: 12px;
}
.task-checkbox input {
display: none;
}
.checkmark {
width: 22px;
height: 22px;
border: 2px solid #d1d5db;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s ease;
}
.checkmark:hover {
border-color: var(--primary);
}
.task-checkbox input:checked + .checkmark {
background: var(--done-color);
border-color: var(--done-color);
}
.task-checkbox input:checked + .checkmark::after {
content: '✓';
color: white;
font-weight: bold;
font-size: 14px;
}
/* Empty State */
.empty-state {
text-align: center;
padding: 40px 20px;
color: rgba(255, 255, 255, 0.5);
}
.empty-state-icon {
font-size: 3rem;
margin-bottom: 15px;
}
.empty-state p {
font-size: 0.95rem;
}
/* Modal Styles */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.6);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
.modal {
background: white;
border-radius: 20px;
padding: 30px;
width: 90%;
max-width: 500px;
transform: scale(0.9);
transition: transform 0.3s ease;
}
.modal-overlay.active .modal {
transform: scale(1);
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 25px;
}
.modal-header h2 {
font-size: 1.5rem;
color: var(--text-dark);
}
.modal-close {
width: 35px;
height: 35px;
border: none;
background: #f3f4f6;
border-radius: 10px;
cursor: pointer;
font-size: 1.2rem;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
}
.modal-close:hover {
background: #e5e7eb;
}
.modal-form {
display: flex;
flex-direction: column;
gap: 20px;
}
.modal-actions {
display: flex;
gap: 15px;
justify-content: flex-end;
margin-top: 10px;
}
.btn-cancel {
padding: 12px 25px;
border: 2px solid #e5e7eb;
background: white;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
transition: all 0.2s ease;
}
.btn-cancel:hover {
background: #f3f4f6;
}
.btn-save {
padding: 12px 25px;
border: none;
background: var(--primary);
color: white;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
transition: all 0.2s ease;
}
.btn-save:hover {
background: var(--primary-dark);
}
/* Toast Notification */
.toast-container {
position: fixed;
bottom: 30px;
right: 30px;
z-index: 2000;
}
.toast {
background: white;
padding: 15px 25px;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
display: flex;
align-items: center;
gap: 12px;
margin-top: 10px;
transform: translateX(120%);
transition: transform 0.3s ease;
}
.toast.show {
transform: translateX(0);
}
.toast-icon {
width: 30px;
height: 30px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.toast.success .toast-icon {
background: #d1fae5;
color: #059669;
}
.toast.error .toast-icon {
background: #fee2e2;
color: #dc2626;
}
.toast.info .toast-icon {
background: #e0e7ff;
color: #4f46e5;
}
/* Clear All Button */
.clear-section {
text-align: center;
margin-top: 30px;
}
.clear-btn {
background: rgba(255, 255, 255, 0.1);
color: #fca5a5;
border: 2px solid rgba(252, 165, 165, 0.3);
padding: 12px 25px;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
transition: all 0.3s ease;
}
.clear-btn:hover {
background: rgba(239, 68, 68, 0.2);
border-color: #ef4444;
color: #ef4444;
}
/* Responsive Design */
@media (max-width: 768px) {
.header h1 {
font-size: 1.8rem;
}
.add-task-form {
flex-direction: column;
}
.add-btn {
width: 100%;
justify-content: center;
}
.stats-bar {
gap: 15px;
}
.stat-item {
padding: 12px 20px;
min-width: 100px;
}
.stat-item .number {
font-size: 1.5rem;
}
.board {
grid-template-columns: 1fr;
}
.task-actions {
opacity: 1;
}
}
/* Animations */
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.task-card {
animation: slideIn 0.3s ease;
}
@keyframes pulse {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.05);
}
}
.column.drag-over {
animation: pulse 0.5s ease infinite;
}
</style>
</head>
<body>
<div class="container">
<!-- Header -->
<header class="header">
<h1>📋 Task<span>Flow</span></h1>
<p>Organize your tasks with drag & drop simplicity</p>
</header>
<!-- Stats Bar -->
<div class="stats-bar">
<div class="stat-item">
<div class="number" id="totalTasks">0</div>
<div class="label">Total</div>
</div>
<div class="stat-item">
<div class="number" id="todoCount">0</div>
<div class="label">To Do</div>
</div>
<div class="stat-item">
<div class="number" id="progressCount">0</div>
<div class="label">In Progress</div>
</div>
<div class="stat-item">
<div class="number" id="doneCount">0</div>
<div class="label">Done</div>
</div>
</div>
<!-- Add Task Section -->
<section class="add-task-section">
<form class="add-task-form" id="addTaskForm">
<div class="input-group" style="flex: 2;">
<label for="taskTitle">Task Title</label>
<input type="text" id="taskTitle" placeholder="What needs to be done?" required>
</div>
<div class="input-group" style="flex: 3;">
<label for="taskDescription">Description (Optional)</label>
<input type="text" id="taskDescription" placeholder="Add some details...">
</div>
<div class="input-group priority-select" style="flex: 1; min-width: 150px;">
<label for="taskPriority">Priority</label>
<select id="taskPriority">
<option value="low">🟢 Low</option>
<option value="medium" selected>🟡 Medium</option>
<option value="high">🔴 High</option>
</select>
</div>
<button type="submit" class="add-btn">
<span>➕</span> Add Task
</button>
</form>
</section>
<!-- Kanban Board -->
<div class="board">
<!-- To Do Column -->
<div class="column todo" data-status="todo">
<div class="column-header">
<div class="column-title">
<div class="column-icon">📝</div>
<span>To Do</span>
</div>
<span class="task-count" id="todoTaskCount">0</span>
</div>
<div class="task-list" id="todoList" data-status="todo">
<!-- Tasks will be added here -->
</div>
</div>
<!-- In Progress Column -->
<div class="column progress" data-status="progress">
<div class="column-header">
<div class="column-title">
<div class="column-icon">🔄</div>
<span>In Progress</span>
</div>
<span class="task-count" id="progressTaskCount">0</span>
</div>
<div class="task-list" id="progressList" data-status="progress">
<!-- Tasks will be added here -->
</div>
</div>
<!-- Done Column -->
<div class="column done" data-status="done">
<div class="column-header">
<div class="column-title">
<div class="column-icon">✅</div>
<span>Done</span>
</div>
<span class="task-count" id="doneTaskCount">0</span>
</div>
<div class="task-list" id="doneList" data-status="done">
<!-- Tasks will be added here -->
</div>
</div>
</div>
<!-- Clear All Button -->
<div class="clear-section">
<button class="clear-btn" id="clearAllBtn">🗑️ Clear All Tasks</button>
</div>
</div>
<!-- Edit Modal -->
<div class="modal-overlay" id="editModal">
<div class="modal">
<div class="modal-header">
<h2>✏️ Edit Task</h2>
<button class="modal-close" id="closeModal">✕</button>
</div>
<form class="modal-form" id="editTaskForm">
<input type="hidden" id="editTaskId">
<div class="input-group">
<label for="editTitle">Task Title</label>
<input type="text" id="editTitle" required>
</div>
<div class="input-group">
<label for="editDescription">Description</label>
<textarea id="editDescription"></textarea>
</div>
<div class="input-group">
<label for="editPriority">Priority</label>
<select id="editPriority">
<option value="low">🟢 Low</option>
<option value="medium">🟡 Medium</option>
<option value="high">🔴 High</option>
</select>
</div>
<div class="input-group">
<label for="editStatus">Status</label>
<select id="editStatus">
<option value="todo">📝 To Do</option>
<option value="progress">🔄 In Progress</option>
<option value="done">✅ Done</option>
</select>
</div>
<div class="modal-actions">
<button type="button" class="btn-cancel" id="cancelEdit">Cancel</button>
<button type="submit" class="btn-save">Save Changes</button>
</div>
</form>
</div>
</div>
<!-- Toast Container -->
<div class="toast-container" id="toastContainer"></div>
<script>
// Task Manager Class
class TaskManager {
constructor() {
this.tasks = this.loadFromStorage();
this.draggedTask = null;
this.init();
}
// Initialize the application
init() {
this.renderAllTasks();
this.updateStats();
this.setupEventListeners();
this.setupDragAndDrop();
}
// Load tasks from localStorage
loadFromStorage() {
const stored = localStorage.getItem('taskflow_tasks');
return stored ? JSON.parse(stored) : [];
}
// Save tasks to localStorage
saveToStorage() {
localStorage.setItem('taskflow_tasks', JSON.stringify(this.tasks));
}
// Generate unique ID
generateId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
// Add new task
addTask(title, description, priority) {
const task = {
id: this.generateId(),
title: title.trim(),
description: description.trim(),
priority,
status: 'todo',
createdAt: new Date().toISOString(),
completed: false
};
this.tasks.push(task);
this.saveToStorage();
this.renderTask(task);
this.updateStats();
this.showToast('Task added successfully!', 'success');
}
// Update task
updateTask(id, updates) {
const index = this.tasks.findIndex(t => t.id === id);
if (index !== -1) {
this.tasks[index] = { ...this.tasks[index], ...updates };
this.saveToStorage();
this.renderAllTasks();
this.updateStats();
this.showToast('Task updated!', 'success');
}
}
// Delete task
deleteTask(id) {
this.tasks = this.tasks.filter(t => t.id !== id);
this.saveToStorage();
const taskElement = document.querySelector(`[data-task-id="${id}"]`);
if (taskElement) {
taskElement.style.transform = 'translateX(100%)';
taskElement.style.opacity = '0';
setTimeout(() => {
taskElement.remove();
this.updateStats();
}, 300);
}
this.showToast('Task deleted!', 'info');
}
// Move task to different status
moveTask(id, newStatus) {
const task = this.tasks.find(t => t.id === id);
if (task && task.status !== newStatus) {
task.status = newStatus;
task.completed = newStatus === 'done';
this.saveToStorage();
this.updateStats();
}
}
// Render single task
renderTask(task) {
const taskList = document.getElementById(`${task.status}List`);
if (!taskList) return;
const taskCard = document.createElement('div');
taskCard.className = `task-card priority-${task.priority}`;
taskCard.setAttribute('draggable', 'true');
taskCard.setAttribute('data-task-id', task.id);
const priorityLabels = {
low: '🟢 Low',
medium: '🟡 Medium',
high: '🔴 High'
};
const formattedDate = new Date(task.createdAt).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric'
});
taskCard.innerHTML = `
<div class="task-header">
<label class="task-checkbox">
<input type="checkbox" ${task.completed ? 'checked' : ''} data-action="complete">
<span class="checkmark"></span>
</label>
<span class="task-title ${task.completed ? 'completed' : ''}">${this.escapeHtml(task.title)}</span>
<div class="task-actions">
<button class="task-action-btn edit-btn" data-action="edit" title="Edit">✏️</button>
<button class="task-action-btn delete-btn" data-action="delete" title="Delete">🗑️</button>
</div>
</div>
${task.description ? `<p class="task-description">${this.escapeHtml(task.description)}</p>` : ''}
<div class="task-footer">
<div class="task-meta">
<span class="task-tag priority-${task.priority}">${priorityLabels[task.priority]}</span>
</div>
<span class="task-date">📅 ${formattedDate}</span>
</div>
`;
taskList.appendChild(taskCard);
this.addTaskCardEvents(taskCard, task);
}
// Render all tasks
renderAllTasks() {
// Clear all lists
document.getElementById('todoList').innerHTML = '';
document.getElementById('progressList').innerHTML = '';
document.getElementById('doneList').innerHTML = '';
// Sort tasks by creation date (newest first) and render
const sortedTasks = [...this.tasks].sort((a, b) =>
new Date(b.createdAt) - new Date(a.createdAt)
);
sortedTasks.forEach(task => this.renderTask(task));
// Show empty states
this.showEmptyStates();
}
// Show empty states for columns
showEmptyStates() {
const columns = ['todo', 'progress', 'done'];
const emptyMessages = {
todo: 'No tasks yet. Add one above!',
progress: 'Drag tasks here to start working',
done: 'Completed tasks will appear here'
};
const emptyIcons = {
todo: '📋',
progress: '🚀',
done: '🎉'
};
columns.forEach(status => {
const list = document.getElementById(`${status}List`);
const tasksInColumn = this.tasks.filter(t => t.status === status);
const existingEmpty = list.querySelector('.empty-state');
if (existingEmpty) existingEmpty.remove();
if (tasksInColumn.length === 0) {
const emptyState = document.createElement('div');
emptyState.className = 'empty-state';
emptyState.innerHTML = `
<div class="empty-state-icon">${emptyIcons[status]}</div>
<p>${emptyMessages[status]}</p>
`;
list.appendChild(emptyState);
}
});
}
// Add event listeners to task card
addTaskCardEvents(taskCard, task) {
taskCard.addEventListener('click', (e) => {
const action = e.target.dataset.action;
if (action === 'complete') {
const newStatus = e.target.checked ? 'done' : 'todo';
this.updateTask(task.id, {
completed: e.target.checked,
status: newStatus
});
} else if (action === 'edit') {
this.openEditModal(task.id);
} else if (action === 'delete') {
if (confirm('Are you sure you want to delete this task?')) {
this.deleteTask(task.id);
}
}
});
// Drag events
taskCard.addEventListener('dragstart', (e) => {
this.draggedTask = task.id;
taskCard.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', task.id);
});
taskCard.addEventListener('dragend', () => {
taskCard.classList.remove('dragging');
this.draggedTask = null;
document.querySelectorAll('.task-list').forEach(list => {
list.classList.remove('drag-over');
});
});
}
// Setup drag and drop for columns
setupDragAndDrop() {
const taskLists = document.querySelectorAll('.task-list');
taskLists.forEach(list => {
list.addEventListener('dragover', (e) => {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
list.classList.add('drag-over');
});
list.addEventListener('dragleave', (e) => {
if (!list.contains(e.relatedTarget)) {
list.classList.remove('drag-over');
}
});
list.addEventListener('drop', (e) => {
e.preventDefault();
list.classList.remove('drag-over');
const taskId = e.dataTransfer.getData('text/plain');
const newStatus = list.dataset.status;
if (taskId && newStatus) {
this.moveTask(taskId, newStatus);
this.renderAllTasks();
}
});
});
}
// Update statistics
updateStats() {
const todoTasks = this.tasks.filter(t => t.status === 'todo');
const progressTasks = this.tasks.filter(t => t.status === 'progress');
const doneTasks = this.tasks.filter(t => t.status === 'done');
document.getElementById('totalTasks').textContent = this.tasks.length;
document.getElementById('todoCount').textContent = todoTasks.length;
document.getElementById('progressCount').textContent = progressTasks.length;
document.getElementById('doneCount').textContent = doneTasks.length;
document.getElementById('todoTaskCount').textContent = todoTasks.length;
document.getElementById('progressTaskCount').textContent = progressTasks.length;
document.getElementById('doneTaskCount').textContent = doneTasks.length;
}
// Open edit modal
openEditModal(taskId) {
const task = this.tasks.find(t => t.id === taskId);
if (!task) return;
document.getElementById('editTaskId').value = task.id;
document.getElementById('editTitle').value = task.title;
document.getElementById('editDescription').value = task.description || '';
document.getElementById('editPriority').value = task.priority;
document.getElementById('editStatus').value = task.status;
document.getElementById('editModal').classList.add('active');
}
// Close edit modal
closeEditModal() {
document.getElementById('editModal').classList.remove('active');
}
// Show toast notification
showToast(message, type = 'info') {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.className = `toast ${type}`;
const icons = {
success: '✓',
error: '✕',
info: 'ℹ'
};
toast.innerHTML = `
<div class="toast-icon">${icons[type]}</div>
<span>${message}</span>
`;
container.appendChild(toast);
// Trigger animation
setTimeout(() => toast.classList.add('show'), 10);
// Remove after delay
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// Clear all tasks
clearAllTasks() {
if (confirm('Are you sure you want to delete all tasks? This cannot be undone.')) {
this.tasks = [];
this.saveToStorage();
this.renderAllTasks();
this.updateStats();
this.showToast('All tasks cleared!', 'info');
}
}
// Escape HTML to prevent XSS
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Setup event listeners
setupEventListeners() {
// Add task form
document.getElementById('addTaskForm').addEventListener('submit', (e) => {
e.preventDefault();
const title = document.getElementById('taskTitle').value;
const description = document.getElementById('taskDescription').value;
const priority = document.getElementById('taskPriority').value;
if (title.trim()) {
this.addTask(title, description, priority);
e.target.reset();
document.getElementById('taskPriority').value = 'medium';
}
});
// Edit form
document.getElementById('editTaskForm').addEventListener('submit', (e) => {
e.preventDefault();
const taskId = document.getElementById('editTaskId').value;
const updates = {
title: document.getElementById('editTitle').value,
description: document.getElementById('editDescription').value,
priority: document.getElementById('editPriority').value,
status: document.getElementById('editStatus').value,
completed: document.getElementById('editStatus').value === 'done'
};
this.updateTask(taskId, updates);
this.closeEditModal();
});
// Modal close buttons
document.getElementById('closeModal').addEventListener('click', () => this.closeEditModal());
document.getElementById('cancelEdit').addEventListener('click', () => this.closeEditModal());
document.getElementById('editModal').addEventListener('click', (e) => {
if (e.target.id === 'editModal') this.closeEditModal();
});
// Clear all button
document.getElementById('clearAllBtn').addEventListener('click', () => this.clearAllTasks());
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
this.closeEditModal();
}
});
}
}
// Initialize the application
const taskManager = new TaskManager();
</script>
</body>
</html>
Simply save the code as an HTML file and open it in any modern browser to start using!