@@ -64,7 +64,7 @@ function render() {
6464 tbody . innerHTML = "" ;
6565
6666 myLibrary . forEach ( ( book , i ) => {
67- const row = table . insertRow ( ) ;
67+ const row = tbody . insertRow ( ) ;
6868
6969 // keep all cells in one place
7070 const titleCell = row . insertCell ( 0 ) ;
@@ -76,19 +76,30 @@ function render() {
7676 titleCell . textContent = book . title ;
7777 authorCell . textContent = book . author ;
7878 pagesCell . textContent = book . pages ;
79- checkCell . textContent = book . check ? "Read" : "Not Read" ;
79+
80+ const readBtn = document . createElement ( "button" ) ;
81+ readBtn . textContent = book . check ? "Read" : "Not Read" ;
82+ readBtn . className = book . check ? "btn btn-success" : "btn btn-secondary" ;
83+ readBtn . addEventListener ( "click" , ( ) => {
84+ book . check = ! book . check ; // toggle
85+ render ( ) ; // re-render table
86+ } ) ;
87+ checkCell . appendChild ( readBtn ) ;
8088
8189 // use helper function for delete button
82- createDeleteCell ( delCell , i ) ;
90+ createDeleteCell ( delCell , book ) ;
8391 } ) ;
8492}
8593
86- function createDeleteCell ( cell , index ) {
94+ function createDeleteCell ( cell , book ) {
8795 const deleteBtn = document . createElement ( "button" ) ;
8896 deleteBtn . textContent = "Delete" ;
8997 deleteBtn . addEventListener ( "click" , ( ) => {
98+ const index = myLibrary . indexOf ( book ) ;
99+ if ( index > - 1 ) {
90100 myLibrary . splice ( index , 1 ) ;
91101 render ( ) ;
102+ }
92103 } ) ;
93104 cell . appendChild ( deleteBtn ) ;
94105}
0 commit comments