@@ -7,21 +7,18 @@ class Book {
77 }
88}
99
10-
1110const library = [
1211 new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ,
1312 new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ,
1413] ;
1514
16-
1715const bookForm = document . getElementById ( "bookForm" ) ;
1816const titleInput = document . getElementById ( "titleInput" ) ;
1917const authorInput = document . getElementById ( "authorInput" ) ;
2018const pagesInput = document . getElementById ( "pagesInput" ) ;
2119const readCheckbox = document . getElementById ( "readCheckbox" ) ;
2220const tableBody = document . querySelector ( "#displayTable tbody" ) ;
2321
24-
2522bookForm . addEventListener ( "submit" , ( event ) => {
2623 event . preventDefault ( ) ;
2724
@@ -30,73 +27,77 @@ bookForm.addEventListener("submit", (event) => {
3027 const pages = Number ( pagesInput . value ) ;
3128 const read = readCheckbox . checked ;
3229
33-
3430 if ( ! title || ! author || ! Number . isInteger ( pages ) || pages <= 0 ) {
3531 alert ( "Please enter valid book details." ) ;
3632 return ;
3733 }
3834
39-
4035 library . push ( new Book ( title , author , pages , read ) ) ;
41-
42-
4336 bookForm . reset ( ) ;
44-
45-
4637 render ( ) ;
4738} ) ;
4839
49-
5040function render ( ) {
51-
5241 tableBody . innerHTML = "" ;
5342
5443 library . forEach ( ( book , index ) => {
5544 const row = document . createElement ( "tr" ) ;
5645
57- row . innerHTML = `
58- <td>${ book . title } </td>
59- <td>${ book . author } </td>
60- <td>${ book . pages } </td>
61- <td>
62- <button
63- class="btn btn-sm ${ book . read ? "btn-success" : "btn-secondary" } "
64- data-index="${ index } "
65- data-action="toggle"
66- >
67- ${ book . read ? "Yes" : "No" }
68- </button>
69- </td>
70- <td>
71- <button
72- class="btn btn-sm btn-danger"
73- data-index="${ index } "
74- data-action="delete"
75- >
76- Delete
77- </button>
78- </td>
46+ const titleCell = document . createElement ( "td" ) ;
47+ titleCell . textContent = book . title ;
48+
49+ const authorCell = document . createElement ( "td" ) ;
50+ authorCell . textContent = book . author ;
51+
52+ const pagesCell = document . createElement ( "td" ) ;
53+ pagesCell . textContent = book . pages ;
54+
55+ const readCell = document . createElement ( "td" ) ;
56+ readCell . innerHTML = `
57+ <button
58+ class="btn btn-sm ${ book . read ? "btn-success" : "btn-secondary" } "
59+ data-index="${ index } "
60+ data-action="toggle"
61+ >
62+ ${ book . read ? "Yes" : "No" }
63+ </button>
64+ ` ;
65+
66+ const actionCell = document . createElement ( "td" ) ;
67+ actionCell . innerHTML = `
68+ <button
69+ class="btn btn-sm btn-danger"
70+ data-index="${ index } "
71+ data-action="delete"
72+ >
73+ Delete
74+ </button>
7975 ` ;
8076
77+ row . append (
78+ titleCell ,
79+ authorCell ,
80+ pagesCell ,
81+ readCell ,
82+ actionCell
83+ ) ;
84+
8185 tableBody . appendChild ( row ) ;
8286 } ) ;
8387}
8488
85-
8689tableBody . addEventListener ( "click" , ( event ) => {
8790 const button = event . target . closest ( "button" ) ;
8891 if ( ! button ) return ;
8992
9093 const index = Number ( button . dataset . index ) ;
9194 const action = button . dataset . action ;
9295
93-
9496 if ( action === "toggle" ) {
9597 library [ index ] . read = ! library [ index ] . read ;
9698 render ( ) ;
9799 }
98100
99-
100101 if ( action === "delete" ) {
101102 const removedTitle = library [ index ] . title ;
102103 library . splice ( index , 1 ) ;
@@ -105,5 +106,4 @@ tableBody.addEventListener("click", (event) => {
105106 }
106107} ) ;
107108
108-
109109render ( ) ;
0 commit comments