11const listEl = document . getElementById ( "list" ) ;
2- const create_btn_el = document . getElementById ( "create" ) ;
2+ const create_btn_el = document . getElementById ( "create" ) ;
3+ const themeToggleBtn = document . getElementById ( "theme-toggle" ) ;
34
45let todos = [ ] ;
56
67create_btn_el . addEventListener ( "click" , CreateNewTodo ) ;
8+ themeToggleBtn . addEventListener ( "click" , toggleTheme ) ;
79
8- function CreateNewTodo ( ) {
10+ function CreateNewTodo ( ) {
911 const item = {
1012 id : new Date ( ) . getTime ( ) ,
1113 text : "" ,
1214 complete : false
13- }
15+ } ;
1416
1517 todos . unshift ( item ) ;
1618
@@ -26,7 +28,7 @@ function CreateNewTodo () {
2628
2729function createTodoElement ( item ) {
2830 const itemEl = document . createElement ( 'div' ) ;
29- itemEl . classList . add ( "item" )
31+ itemEl . classList . add ( "item" ) ;
3032
3133 const checkbox = document . createElement ( "input" ) ;
3234 checkbox . type = "checkbox" ;
@@ -46,20 +48,20 @@ function createTodoElement(item) {
4648
4749 const edit_btn_el = document . createElement ( "button" ) ;
4850 edit_btn_el . classList . add ( "FA-icons" , "edit-btn" ) ;
49- edit_btn_el . innerHTML = "<i class='fa fa-pen'></i>"
50-
51+ edit_btn_el . innerHTML = "<i class='fa fa-pen'></i>" ;
52+
5153 const remove_btn_el = document . createElement ( "button" ) ;
52- edit_btn_el . classList . add ( "FA-icons" , "remove-btn" ) ;
54+ remove_btn_el . classList . add ( "FA-icons" , "remove-btn" ) ;
5355 remove_btn_el . innerHTML = "<i class='fa fa-circle-minus'></i>" ;
5456
5557 actionsEl . append ( edit_btn_el ) ;
5658 actionsEl . append ( remove_btn_el ) ;
57-
59+
5860 itemEl . append ( checkbox ) ;
5961 itemEl . append ( inputEl ) ;
6062 itemEl . append ( actionsEl ) ;
6163
62- // MY EVENTS
64+ // Event Listeners
6365 checkbox . addEventListener ( "change" , ( ) => {
6466 item . complete = checkbox . checked ;
6567
@@ -92,34 +94,79 @@ function createTodoElement(item) {
9294 itemEl . remove ( ) ;
9395
9496 save ( ) ;
95- } )
97+ } ) ;
9698
97- return { itemEl, inputEl, edit_btn_el, remove_btn_el } ;
99+ return { itemEl, inputEl, edit_btn_el, remove_btn_el } ;
98100}
99101
100- function DisplayTodos ( ) {
101- load ( ) ;
102+ function DisplayTodos ( ) {
103+ load ( ) ;
102104
103- for ( let i = 0 ; i < todos . length ; i ++ ) {
104- const item = todos [ i ] ;
105+ const fragment = document . createDocumentFragment ( ) ;
105106
107+ todos . forEach ( item => {
106108 const { itemEl } = createTodoElement ( item ) ;
109+ fragment . appendChild ( itemEl ) ;
110+ } ) ;
107111
108- listEl . append ( itemEl ) ;
109- }
112+ listEl . appendChild ( fragment ) ;
110113}
111114
112115DisplayTodos ( ) ;
113116
114117function save ( ) {
115- const save = JSON . stringify ( todos ) ;
116-
117- localStorage . setItem ( "my_todos" , save ) ;
118+ try {
119+ const save = JSON . stringify ( todos ) ;
120+ localStorage . setItem ( "my_todos" , save ) ;
121+ } catch ( error ) {
122+ console . error ( "Error saving to localStorage" , error ) ;
123+ }
118124}
119125
120126function load ( ) {
121- const data = localStorage . getItem ( "my_todos" ) ;
122- if ( data ) {
123- todos = JSON . parse ( data ) ;
127+ try {
128+ const data = localStorage . getItem ( "my_todos" ) ;
129+ if ( data ) {
130+ todos = JSON . parse ( data ) ;
131+ }
132+ } catch ( error ) {
133+ console . error ( "Error loading from localStorage" , error ) ;
134+ }
135+ }
136+
137+ function toggleTheme ( ) {
138+ document . body . classList . toggle ( "dark-theme" ) ;
139+ }
140+
141+ // CSS for responsiveness and themes
142+ const style = document . createElement ( 'style' ) ;
143+ style . innerHTML = `
144+ .item {
145+ display: flex;
146+ justify-content: space-between;
147+ align-items: center;
148+ padding: 10px;
149+ border-bottom: 1px solid #ccc;
150+ }
151+ .actions {
152+ display: flex;
153+ gap: 10px;
154+ }
155+ .dark-theme {
156+ background-color: #333;
157+ color: #fff;
158+ }
159+ .dark-theme .item {
160+ border-bottom: 1px solid #555;
161+ }
162+ @media (max-width: 600px) {
163+ .item {
164+ flex-direction: column;
165+ align-items: flex-start;
166+ }
167+ .actions {
168+ margin-top: 10px;
169+ }
124170 }
125- }
171+ ` ;
172+ document . head . appendChild ( style ) ;
0 commit comments