File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments