@@ -2,7 +2,6 @@ let myLibrary = [];
22
33window . addEventListener ( "load" , function ( e ) {
44 populateStorage ( ) ;
5- render ( ) ;
65} ) ;
76
87function populateStorage ( ) {
@@ -20,24 +19,25 @@ function populateStorage() {
2019 }
2120}
2221
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
22+ const titleDom = document . getElementById ( "title" ) ;
23+ const authorDom = document . getElementById ( "author" ) ;
24+ const pagesDom = document . getElementById ( "pages" ) ;
25+ const checkDom = document . getElementById ( "check" ) ;
26+
27+ document . getElementById ( 'submit-btn' ) . addEventListener ( 'click' , submit ) ;
2728
2829//check the right input from forms and if its ok -> add the new book (object in array)
2930//via Book function and start render function
3031function submit ( ) {
3132 if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
33+ titleDom . value == "" ||
34+ authorDom . value == "" ||
35+ pagesDom . value == 0
3636 ) {
3737 alert ( "Please fill all fields!" ) ;
3838 return false ;
3939 } else {
40- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
40+ let book = new Book ( titleDom . value . trim ( ) , authorDom . value . trim ( ) , pagesDom . value , checkDom . checked ) ;
4141 myLibrary . push ( book ) ;
4242 render ( ) ;
4343 }
@@ -52,11 +52,9 @@ function Book(title, author, pages, check) {
5252
5353function render ( ) {
5454 let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
5655 //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
58- table . deleteRow ( n ) ;
59- }
56+ let tableBody = table . querySelector ( "tbody" ) ;
57+ tableBody . textContent = '' ;
6058 //insert updated row and cells
6159 let length = myLibrary . length ;
6260 for ( let i = 0 ; i < length ; i ++ ) {
@@ -66,9 +64,9 @@ function render() {
6664 let pagesCell = row . insertCell ( 2 ) ;
6765 let wasReadCell = row . insertCell ( 3 ) ;
6866 let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
67+ titleCell . textContent = myLibrary [ i ] . title ;
68+ authorCell . textContent = myLibrary [ i ] . author ;
69+ pagesCell . textContent = myLibrary [ i ] . pages ;
7270
7371 //add and wait for action for read/unread button
7472 let changeBut = document . createElement ( "button" ) ;
@@ -81,7 +79,7 @@ function render() {
8179 } else {
8280 readStatus = "Yes" ;
8381 }
84- changeBut . innerText = readStatus ;
82+ changeBut . textContent = readStatus ;
8583
8684 changeBut . addEventListener ( "click" , function ( ) {
8785 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
@@ -93,10 +91,10 @@ function render() {
9391 delBut . id = i + 5 ;
9492 deleteCell . appendChild ( delBut ) ;
9593 delBut . className = "btn btn-warning" ;
96- delBut . innerHTML = "Delete" ;
94+ delBut . textContent = "Delete" ;
9795 delBut . addEventListener ( "click" , function ( ) {
98- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
9996 myLibrary . splice ( i , 1 ) ;
97+ alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
10098 render ( ) ;
10199 } ) ;
102100 }
0 commit comments