Skip to content

Commit 844f547

Browse files
Create index.html for a to-do list application with local storage functionality
1 parent 0320f12 commit 844f547

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>To-Do List</title>
7+
<script>
8+
// Function to add a new task
9+
function addTask() {
10+
const taskInput = document.getElementById('taskInput');
11+
const taskValue = taskInput.value;
12+
if (taskValue) {
13+
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
14+
tasks.push(taskValue);
15+
localStorage.setItem('tasks', JSON.stringify(tasks));
16+
taskInput.value = '';
17+
renderTasks();
18+
}
19+
}
20+
21+
// Function to render tasks from local storage
22+
function renderTasks() {
23+
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
24+
const taskList = document.getElementById('taskList');
25+
taskList.innerHTML = '';
26+
tasks.forEach((task, index) => {
27+
const listItem = document.createElement('li');
28+
listItem.textContent = task;
29+
taskList.appendChild(listItem);
30+
});
31+
}
32+
33+
// Load tasks on page load
34+
window.onload = renderTasks;
35+
</script>
36+
</head>
37+
<body>
38+
<h1>To-Do List</h1>
39+
<input type="text" id="taskInput" placeholder="Add a new task" />
40+
<button onclick="addTask()">Add Task</button>
41+
<ul id="taskList"></ul>
42+
</body>
43+
</html>

0 commit comments

Comments
 (0)