TO DO LIST APPLICATION IN HTML CSS AND JAVASCRIPT

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List App</title>
<style>
    body {
        font-family: Arial, sans-serif;
        background-color: #f4f4f4;
        color: #333;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
    }
    .container {
        background: white;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        width: 300px;
        text-align: center;
    }
    h1 {
        font-size: 24px;
        margin-bottom: 10px;
    }
    input[type="text"] {
        width: 70%;
        padding: 10px;
        margin-bottom: 10px;
        border: 1px solid #ccc;
        border-radius: 4px;
    }
    button {
        padding: 10px;
        border: none;
        border-radius: 4px;
        background-color: #5cb85c;
        color: white;
        cursor: pointer;
    }
    button:hover {
        background-color: #4cae4c;
    }
    ul {
        list-style-type: none;
        padding: 0;
        margin: 10px 0;
    }
    li {
        padding: 10px;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .completed {
        text-decoration: line-through;
        color: #999;
    }
    .filter-buttons {
        margin-top: 10px;
    }
    .filter-buttons button {
        background-color: #007bff;
        margin: 5px;
    }
    .filter-buttons button.active {
        background-color: #0056b3;
    }
</style>
</head>
<body>
<div class="container">
    <h1>To-Do List</h1>
    <input type="text" id="taskInput" placeholder="Add a new task...">
    <button id="addTaskButton">Add Task</button>
    <ul id="taskList"></ul>
    <div class="filter-buttons">
        <button id="allButton" class="active">All</button>
        <button id="activeButton">Active</button>
        <button id="completedButton">Completed</button>
    </div>
</div>

<script>
    const taskInput = document.getElementById("taskInput");
    const addTaskButton = document.getElementById("addTaskButton");
    const taskList = document.getElementById("taskList");
    const allButton = document.getElementById("allButton");
    const activeButton = document.getElementById("activeButton");
    const completedButton = document.getElementById("completedButton");

    let tasks = JSON.parse(localStorage.getItem("tasks")) || [];

    function renderTasks(filter) {
        taskList.innerHTML = "";
        tasks.forEach((task, index) => {
            if (filter === "all" || (filter === "active" && !task.completed) || (filter === "completed" && task.completed)) {
                const li = document.createElement("li");
                li.innerText = task.text;
                if (task.completed) {
                    li.classList.add("completed");
                }
                li.addEventListener("click", () => toggleTaskCompletion(index));
                const deleteButton = document.createElement("button");
                deleteButton.innerText = "Delete";
                deleteButton.addEventListener("click", (e) => {
                    e.stopPropagation();
                    deleteTask(index);
                });
                li.appendChild(deleteButton);
                taskList.appendChild(li);
            }
        });
    }

    function addTask() {
        const taskText = taskInput.value.trim();
        if (taskText) {
            tasks.push({ text: taskText, completed: false });
            taskInput.value = "";
            saveTasks();
            renderTasks(getActiveFilter());
        }
    }

    function deleteTask(index) {
        tasks.splice(index, 1);
        saveTasks();
        renderTasks(getActiveFilter());
    }

    function toggleTaskCompletion(index) {
        tasks[index].completed = !tasks[index].completed;
        saveTasks();
        renderTasks(getActiveFilter());
    }

    function saveTasks() {
        localStorage.setItem("tasks", JSON.stringify(tasks));
    }

    function getActiveFilter() {
        return document.querySelector(".filter-buttons button.active").id.replace("Button", "").toLowerCase();
    }

    addTaskButton.addEventListener("click", addTask);

    allButton.addEventListener("click", () => {
        allButton.classList.add("active");
        activeButton.classList.remove("active");
        completedButton.classList.remove("active");
        renderTasks("all");
    });

    activeButton.addEventListener("click", () => {
        allButton.classList.remove("active");
        activeButton.classList.add("active");
        completedButton.classList.remove("active");
        renderTasks("active");
    });

    completedButton.addEventListener("click", () => {
        allButton.classList.remove("active");
        activeButton.classList.remove("active");
        completedButton.classList.add("active");
        renderTasks("completed");
    });

    // Initial render
    renderTasks("all");
</script>
</body>
</html>
JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *


error: Content is protected !!
Scroll to Top
MacroNepal
Verified by MonsterInsights