Display and manage a to-do list with tasks and completion status.

by velo_ninja

HTML

<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
  <h1>My To-Do List</h1>
  <input type="text" id="newTask" placeholder="Add a new task...">
  <button id="addTask">Add Task</button>
  <ul id="taskList"></ul>
  <script src="script.js"></script>
</body>
</html>

CSS

body {
  font-family: sans-serif;
}

#taskList {
  list-style-type: none;
  padding: 0;
}

#taskList li {
  margin-bottom: 5px;
  padding: 10px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
}

#taskList li.completed {
  text-decoration: line-through;
  opacity: 0.5;
}

JavaScript

const newTaskInput = document.getElementById('newTask');
const addTaskButton = document.getElementById('addTask');
const taskList = document.getElementById('taskList');

// Load tasks from local storage on page load
function loadTasks() {
const storedTasks = localStorage.getItem('tasks');
if (storedTasks) {
const tasks = JSON.parse(storedTasks);
tasks.forEach(task => addTaskToList(task));
}
}

// Add a task to the list and local storage
function addTask(taskText) {
const newTask = { text: taskText, completed: false };
addTaskToList(newTask);
saveTasks();
}