@@ -21,15 +21,21 @@ <h2>LOCAL TAPAS</h2>
2121 </ ul >
2222 < form class ="add-items ">
2323 < input type ="text " name ="item " placeholder ="Item Name " required >
24- < input type ="submit " value ="+ Add Item ">
25- </ form >
24+ < input type ="submit " value ="+ Add Item ">
25+ </ form >
26+ < p class ="all-items ">
27+ < input type ="submit " value ="Check All ">
28+ < input type ="submit " value ="Clear All ">
29+ </ p >
2630 </ div >
2731
2832< script >
2933 const addItems = document . querySelector ( '.add-items' ) ;
3034 const itemsList = document . querySelector ( '.plates' ) ;
3135 const items = JSON . parse ( localStorage . getItem ( 'items' ) ) || [ ] ;
3236
37+ const allItems = document . querySelector ( '.all-items' ) ;
38+
3339 function addItem ( e ) {
3440 // prevent the page from reloading (which is the default action)
3541 e . preventDefault ( ) ;
@@ -69,10 +75,10 @@ <h2>LOCAL TAPAS</h2>
6975
7076 function toggleDone ( e ) {
7177 // fall out if the target is not an input
72- if ( ! e . target . matches ( 'input' ) ) return ;
78+ if ( ! e . target . matches ( 'input' ) ) return ; // Element.matches()
7379 console . log ( e . target ) ;
7480
75- // loop through our items array and find the one which has been checked.
81+ // find the item which has been checked.
7682 const el = e . target ;
7783 console . log ( el . dataset . index ) ;
7884 const index = el . dataset . index ;
@@ -84,12 +90,37 @@ <h2>LOCAL TAPAS</h2>
8490 populateList ( itemsList , items ) ;
8591 }
8692
93+ function toggleAllItems ( e ) {
94+
95+ const el = e . target ;
96+
97+ if ( el . value . match ( 'Check All' ) ) {
98+ // loop through items array and check all that need to be checked.
99+ items . forEach ( function ( item ) {
100+ // console.log(item);
101+ item . done = true ;
102+ } ) ;
103+ } else {
104+ items . forEach ( function ( item ) {
105+ // console.log(item);
106+ item . done = false ;
107+ } ) ;
108+ }
109+
110+ // update local storage
111+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
112+ // update the HTML
113+ populateList ( itemsList , items ) ;
114+ }
115+
87116 // listen for the submit event to cover all bases.
88117 addItems . addEventListener ( 'submit' , addItem ) ;
89118
90119 // listen for a click on the items list. Via event delegation, have the items list pass the event on to it's children.
91120 itemsList . addEventListener ( 'click' , toggleDone ) ;
92121
122+ allItems . addEventListener ( 'click' , toggleAllItems ) ;
123+
93124 // load from local storage..
94125 populateList ( itemsList , items ) ;
95126
0 commit comments