@@ -2,7 +2,6 @@ let myLibrary = [];
22
33window . addEventListener ( "load" , function ( e ) {
44 populateStorage ( ) ;
5- render ( ) ;
65} ) ;
76
87function populateStorage ( ) {
@@ -20,35 +19,39 @@ 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 titleInput = document . getElementById ( "title" ) ;
23+ const authorInput = document . getElementById ( "author" ) ;
24+ const pagesInput = document . getElementById ( "pages" ) ;
25+ const checkInput = document . getElementById ( "check" ) ;
2726
2827//check the right input from forms and if its ok -> add the new book (object in array)
2928//via Book function and start render function
3029function submit ( ) {
30+ const titleVal = titleInput . value . trim ( ) ;
31+ const authorVal = authorInput . value . trim ( ) ;
32+ const pagesVal = parseInt ( pagesInput . value , 10 ) ;
33+
3134 if (
32- title . value == null ||
33- title . value == "" ||
34- author . value == null ||
35- author . value == "" ||
36- pages . value == null ||
37- pages . value == ""
35+ titleVal == null ||
36+ titleVal == "" ||
37+ authorVal == null ||
38+ authorVal == "" ||
39+ isNaN ( pagesVal ) ||
40+ pagesVal <= 0
3841 ) {
3942 alert ( "Please fill all fields!" ) ;
4043 return false ;
4144 } else {
42- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
45+ let book = new Book ( titleVal , authorVal , pagesVal , checkInput . checked ) ;
4346 myLibrary . push ( book ) ;
4447 render ( ) ;
4548 }
4649
4750 // clear the form after submit
48- title . value = "" ;
49- author . value = "" ;
50- pages . value = "" ;
51- check . checked = false ;
51+ titleInput . value = "" ;
52+ authorInput . value = "" ;
53+ pagesInput . value = "" ;
54+ checkInput . checked = false ;
5255}
5356
5457function Book ( title , author , pages , check ) {
@@ -74,9 +77,9 @@ function render() {
7477 let pagesCell = row . insertCell ( 2 ) ;
7578 let wasReadCell = row . insertCell ( 3 ) ;
7679 let deleteCell = row . insertCell ( 4 ) ;
77- titleCell . innerHTML = myLibrary [ i ] . title ;
78- authorCell . innerHTML = myLibrary [ i ] . author ;
79- pagesCell . innerHTML = myLibrary [ i ] . pages ;
80+ titleCell . textContent = myLibrary [ i ] . title ;
81+ authorCell . textContent = myLibrary [ i ] . author ;
82+ pagesCell . textContent = myLibrary [ i ] . pages ;
8083
8184 //add and wait for action for read/unread button
8285 let changeBut = document . createElement ( "button" ) ;
@@ -89,7 +92,7 @@ function render() {
8992 } else {
9093 readStatus = "No" ;
9194 }
92- changeBut . innerText = readStatus ;
95+ changeBut . textContent = readStatus ;
9396
9497 changeBut . addEventListener ( "click" , function ( ) {
9598 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
@@ -101,7 +104,7 @@ function render() {
101104 delButton . id = i + 5 ;
102105 deleteCell . appendChild ( delButton ) ;
103106 delButton . className = "btn btn-warning" ;
104- delButton . innerHTML = "Delete" ;
107+ delButton . textContent = "Delete" ;
105108 delButton . addEventListener ( "click" , function ( ) {
106109 alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
107110 myLibrary . splice ( i , 1 ) ;
0 commit comments