Skip to content

Commit 64e5908

Browse files
committed
took the eventhandler off from html, and trim pagesValue
1 parent 34fe6f0 commit 64e5908

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

debugging/book-library/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1>Library</h1>
2828
</button>
2929

3030
<div id="demo" class="collapse">
31-
<form class="form-group" id="book-form" onsubmit="handleSubmit(event)" >
31+
<form class="form-group" id="book-form">
3232
<label for="title">Title:</label>
3333
<input
3434
type="text"

debugging/book-library/script.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,29 @@ const checkInput = document.getElementById("check");
2828
//via Book function and start render function
2929
function handleSubmit(event) {
3030
event.preventDefault();
31+
let titleValue = titleInput.value.trim();
32+
let authorValue = authorInput.value.trim();
33+
let pagesValue = Number(pagesInput.value.trim());
34+
let read = checkInput.checked;
3135
if (
32-
!titleInput.value.trim() ||
33-
!authorInput.value.trim() ||
34-
pagesInput.value <= 0
36+
!titleValue||
37+
!authorValue ||
38+
!Number.isInteger(pagesValue) ||
39+
Number(pagesValue) <= 0
3540
) {
36-
alert("Please fill all fields!");
41+
alert("Please fill all fields! Page count must be a whole number above 0.");
3742
return false;
3843
} else {
39-
let book = new Book(String(titleInput.value), String(authorInput.value), Number(pagesInput.value), checkInput.checked);
44+
let book = new Book(titleValue, authorValue, pagesValue, read);
4045
myLibrary.push(book);
4146
resetAddNewBook()
4247
render();
4348
}
4449
}
4550

51+
const bookForm = document.getElementById("book-form")
52+
bookForm.addEventListener("submit", (event) => handleSubmit(event));
53+
4654
function resetAddNewBook() {
4755
document.getElementById("book-form").reset()
4856
}
@@ -58,7 +66,7 @@ class Book {
5866

5967
function render() {
6068
//delete old table
61-
let tableBody = document.getElementById("display-body")
69+
const tableBody = document.getElementById("display-body")
6270
tableBody.innerHTML = '';
6371
//insert updated row and cells
6472
myLibrary.forEach((book, index) => {

0 commit comments

Comments
 (0)