Low
Medium
High
Add Task
<div class="filter-buttons">
<button onclick="filterTasks('all')">All</button>
<button onclick="filterTasks('active')">Active</button>
<button onclick="filterTasks('completed')">Completed</button>
</div>
<ul id="taskList"></ul>
</div>
<script>
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
let currentFilter = 'all'; // 'all', 'active', or 'completed'
// Function to add a task
function addTask() {
const content = document.getElementById('taskInput').value;
const priority = document.getElementById('prioritySelect').value;
if (!content) return;
const newTask = { content: content, completed: false, priority: priority };
tasks.push(newTask);
saveTasks();
renderTasks();
document.getElementById('taskInput').value = '';
}
// Function to delete a task
function deleteTask(index) {
tasks.splice(index, 1);
saveTasks();
renderTasks();
}
// Function to toggle task completion
function toggleTask(index) {
tasks[index].completed = !tasks[index].completed;
saveTasks();
renderTasks();
}
// Function to edit a task
function editTask(index) {
const newContent = prompt("Edit Task:", tasks[index].content);
if (newContent !== null && newContent.trim() !== "") {
tasks[index].content = newContent;
saveTasks();
renderTasks();
}
}
// Function to render tasks based on the current filter
function renderTasks() {
const taskList = document.getElementById('taskList');
taskList.innerHTML = '';
const filteredTasks = tasks.filter(task => {
if (currentFilter === 'active') return !task.completed;
if (currentFilter === 'completed') return task.completed;
return true; // 'all' filter
});
filteredTasks.forEach((task, index) => {
const li = document.createElement('li');
li.className = task.completed ? 'completed' : '';
li.innerHTML = `
<span onclick="toggleTask(${index})">${task.content}</span>
<span class="priority" style="color: ${getPriorityColor(task.priority)}">${task.priority.charAt(0).toUpperCase() + task.priority.slice(1)}</span>
<button onclick="editTask(${index})">Edit</button>
<button onclick="deleteTask(${index})">Delete</button>
`;
taskList.appendChild(li);
});
}
// Function to get the color for the task's priority
function getPriorityColor(priority) {
switch (priority) {
case 'high': return 'red';
case 'medium': return 'orange';
case 'low': return 'green';
default: return 'black';
}
}
// Function to filter tasks based on status
function filterTasks(status) {
currentFilter = status;
renderTasks();
}
// Function to save tasks to local storage
function saveTasks() {
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// Initial render of tasks
renderTasks();
</script>