Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title> </title>
<title>My Book Library</title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
charset="utf-8" />
<meta name="description" content="Keep track of the books you've read." />
<meta content="width=device-width, initial-scale=1.0"
/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js" defer></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" defer></script>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
Expand All @@ -18,28 +18,29 @@
</head>

<body>
<main>
<div class="jumbotron text-center">
<h1>Library</h1>
<p>Add books to your virtual library</p>
</div>

<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
<button data-toggle="collapse" data-target="#demo" class="btn btn-info" aria-expanded="false" aria-controls="demo">
Add new book
</button>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
Comment on lines 37 to 40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The browser checks the input elements against the specified constraints only when a user submits a form.
Without <form>, the browser won't enforce the "required" constraint.

<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
Expand Down Expand Up @@ -90,7 +91,7 @@ <h1>Library</h1>
</tr>
</tbody>
</table>

<script src="script.js"></script>
</main>
<script defer src="script.js"></script>
</body>
</html>
</html>
24 changes: 16 additions & 8 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const check = document.getElementById("check");
//via Book function and start render function
function submit() {
if (
author.value == null ||
author.value == "" ||
title.value == null ||
title.value == "" ||
pages.value == null ||
Expand All @@ -37,8 +39,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();
}
}
Expand All @@ -54,13 +56,14 @@ 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--) {
// there was a syntax error here the ")" was missing
table.deleteRow(n);
}
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = table.insertRow(-1);
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
Expand All @@ -76,7 +79,7 @@ function render() {
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
if (myLibrary[i].check == true) {
readStatus = "Yes";
} else {
readStatus = "No";
Expand All @@ -89,15 +92,20 @@ 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 () {
alert(`You've deleted title: ${myLibrary[i].title}`);
delBut.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title;

myLibrary.splice(i, 1);
render();

setTimeout(function () {
alert(`You've deleted title: ${deletedTitle}`);
}, 0.5);
});
}
}
Loading