Skip to content

TODO-App Musterlösung (Vanilla JavaScript)

Dies ist die vollständige Musterlösung für die TODO-App mit Vanilla JavaScript.

Verwendung

  1. Kopiere den gesamten Code unten
  2. Erstelle eine neue HTML-Datei (z.B. todo-app.html)
  3. Füge den Code ein und speichere die Datei
  4. Öffne die Datei in deinem Browser

Code

html
<!DOCTYPE html>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>TODO App - Vanilla JavaScript</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f5f5f5;
      padding: 20px;
    }

    .container {
      max-width: 500px;
      margin: 50px auto;
      background: white;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }

    h1 {
      color: #333;
      text-align: center;
      margin-bottom: 30px;
      font-size: 32px;
    }

    #todoInput {
      width: 100%;
      padding: 12px;
      border: 2px solid #ddd;
      border-radius: 5px;
      font-size: 16px;
      transition: border-color 0.3s, box-shadow 0.3s;
    }

    #todoInput:focus {
      outline: none;
      border-color: #4CAF50;
      box-shadow: 0 0 5px rgba(76, 175, 80, 0.5);
    }

    #addBtn {
      width: 100%;
      background-color: #4CAF50;
      color: white;
      font-size: 16px;
      padding: 12px;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      margin-top: 15px;
      transition: background-color 0.2s;
    }

    #addBtn:hover {
      background-color: #45a049;
    }

    #todoList {
      list-style: none;
      padding: 0;
      margin-top: 20px;
    }

    .todo-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #f9f9f9;
      padding: 12px 15px;
      margin-bottom: 10px;
      border-radius: 5px;
      border: 1px solid #ddd;
      transition: all 0.3s ease;
    }

    .todo-item:hover {
      transform: translateX(5px);
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    .todo-item span {
      flex: 1;
      transition: all 0.3s;
    }

    .complete-btn,
    .delete-btn {
      padding: 5px 10px;
      border: none;
      border-radius: 3px;
      cursor: pointer;
      margin-left: 5px;
      transition: background-color 0.2s;
    }

    .complete-btn {
      background-color: #4CAF50;
      color: white;
    }

    .complete-btn:hover {
      background-color: #45a049;
    }

    .complete-btn:disabled {
      background-color: #ccc;
      cursor: not-allowed;
    }

    .delete-btn {
      background-color: #f44336;
      color: white;
    }

    .delete-btn:hover {
      background-color: #da190b;
    }

    .empty-message {
      text-align: center;
      color: #999;
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <div id="app" class="container">
    <h1>Meine TODO-Liste</h1>
    <input type="text" id="todoInput" placeholder="Neue Aufgabe eingeben...">
    <button id="addBtn">Hinzufügen</button>
    <ul id="todoList"></ul>
    <p id="emptyMessage" class="empty-message">Keine Aufgaben vorhanden. Füge eine neue hinzu!</p>
  </div>

  <script>
    // Element-Referenzen holen
    const todoInput = document.getElementById("todoInput");
    const addBtn = document.getElementById("addBtn");
    const todoList = document.getElementById("todoList");
    const emptyMessage = document.getElementById("emptyMessage");

    // Event Listener für Button
    addBtn.addEventListener("click", addTodo);

    // Event Listener für Enter-Taste
    todoInput.addEventListener("keypress", function(event) {
      if (event.key === "Enter") {
        addTodo();
      }
    });

    // addTodo Funktion
    function addTodo() {
      // Input-Wert prüfen
      const todoText = todoInput.value.trim();
      if (todoText === "") {
        alert("Bitte Aufgabe eingeben!");
        return;
      }

      // TODO-Element erstellen
      const li = document.createElement("li");
      li.className = "todo-item";

      // TODO-Inhalt mit Span und Buttons
      const span = document.createElement("span");
      span.textContent = todoText;
      li.appendChild(span);

      // Complete Button
      const completeBtn = document.createElement("button");
      completeBtn.textContent = "✓";
      completeBtn.className = "complete-btn";

      // Erledigt-Funktion
      completeBtn.addEventListener("click", function() {
        span.style.textDecoration = "line-through";
        span.style.color = "#888";
        completeBtn.disabled = true;
      });

      li.appendChild(completeBtn);

      // Delete Button
      const deleteBtn = document.createElement("button");
      deleteBtn.textContent = "✗";
      deleteBtn.className = "delete-btn";

      // Löschen-Funktion
      deleteBtn.addEventListener("click", function() {
        li.remove();
        // Leere-Liste Nachricht aktualisieren
        if (todoList.children.length === 0) {
          emptyMessage.style.display = "block";
        }
      });

      li.appendChild(deleteBtn);

      // TODO zur Liste hinzufügen
      todoList.appendChild(li);

      // Input leeren
      todoInput.value = "";
      todoInput.focus();

      // Leere-Liste Nachricht verstecken
      emptyMessage.style.display = "none";
    }
  </script>
</body>
</html>

Erklärung

Diese Lösung enthält:

  • HTML-Struktur: Container mit Überschrift, Input-Feld, Button und TODO-Liste
  • CSS-Styling: Professionelles Design mit Transitions, Hover-Effekten und Flexbox
  • JavaScript-Logik:
    • DOM-Manipulation zum Erstellen und Entfernen von Elementen
    • Event Listener für Button-Klicks und Enter-Taste
    • Dynamische Erstellung von TODO-Items mit Complete/Delete Funktionalität
    • Leere-Liste Nachricht die automatisch ein-/ausgeblendet wird

Zurück zu den Aufgaben

Informatik & ICT Unterricht Neufeld