• Home
  • /
  • To-Do List App using HTML, CSS & JavaScript

To-Do List App using HTML, CSS & JavaScript

HTML

				
					<!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 | Learn With Arshyan</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f2f2f2;
      display: flex;
      justify-content: center;
      align-items: start;
      padding: 50px 20px;
    }

    .todo-container {
      background: #fff;
      width: 100%;
      max-width: 400px;
      padding: 20px;
      border-radius: 10px;
      box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    }

    .todo-container h2 {
      text-align: center;
      margin-bottom: 15px;
    }

    .input-section {
      display: flex;
      gap: 10px;
      margin-bottom: 20px;
    }

    .input-section input {
      flex: 1;
      padding: 10px;
      border-radius: 5px;
      border: 1px solid #ccc;
    }

    .input-section button {
      padding: 10px 15px;
      background: #007bff;
      color: #fff;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }

    ul {
      list-style: none;
      padding: 0;
    }

    ul li {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      border-bottom: 1px solid #eee;
    }

    ul li.completed span {
      text-decoration: line-through;
      color: gray;
    }

    ul li button {
      background: #dc3545;
      color: #fff;
      border: none;
      padding: 5px 10px;
      border-radius: 4px;
      cursor: pointer;
    }

    @media(max-width: 480px) {
      .input-section {
        flex-direction: column;
      }

      .input-section button {
        width: 100%;
      }
    }
  </style>
</head>
<body>

  <div class="todo-container">
    <h2>To-Do List</h2>
    <div class="input-section">
      <input type="text" id="taskInput" placeholder="Enter a new task">
      <button onclick="addTask()">Add</button>
    </div>
    <ul id="taskList"></ul>
  </div>

  <script>
    const taskInput = document.getElementById("taskInput");
    const taskList = document.getElementById("taskList");

    // Load tasks from localStorage
    window.onload = function() {
      const saved = localStorage.getItem("tasks");
      if (saved) {
        taskList.innerHTML = saved;
      }
    };

    // Save current tasks to localStorage
    function saveTasks() {
      localStorage.setItem("tasks", taskList.innerHTML);
    }

    // Add a new task
    function addTask() {
      const taskText = taskInput.value.trim();
      if (taskText === "") return;

      const li = document.createElement("li");

      const span = document.createElement("span");
      span.textContent = taskText;
      span.onclick = () => {
        li.classList.toggle("completed");
        saveTasks();
      };

      const delBtn = document.createElement("button");
      delBtn.textContent = "Delete";
      delBtn.onclick = () => {
        taskList.removeChild(li);
        saveTasks();
      };

      li.appendChild(span);
      li.appendChild(delBtn);
      taskList.appendChild(li);

      taskInput.value = "";
      saveTasks();
    }
  </script>

</body>
</html>

				
			

Share this post

Scroll to Top