diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..e24a68f3 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -65,7 +65,7 @@

Library

type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + id ="submit-btn" /> diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..c7da752c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,5 +1,4 @@ let myLibrary = []; - window.addEventListener("load", function (e) { populateStorage(); render(); @@ -16,7 +15,6 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -24,6 +22,12 @@ const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); +const submitBtn = document.getElementById("submit-btn"); + +submitBtn.addEventListener("click", function (event) { + event.preventDefault(); + submit(); +}); //check the right input from forms and if its ok -> add the new book (object in array) //via Book function and start render function @@ -37,8 +41,8 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); } } @@ -54,9 +58,9 @@ function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + for (let n = rowsNumber - 1; n > 0; n--){ table.deleteRow(n); - } + } //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { @@ -77,9 +81,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; @@ -89,15 +93,16 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); + let delBut = document.createElement("button"); delBut.id = i + 5; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); } } +