-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
52 lines (47 loc) · 1.52 KB
/
javascript.js
File metadata and controls
52 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const newBook = document.querySelector("#new");
const dialogElement = document.querySelector("dialog");
const bookCards = document.querySelector(".book-card");
const submit = document.querySelector("#submit");
const bookContainer = document.querySelector("#book-container");
const form = document.querySelector("form");
const myLibrary = [];
function Book(title, author, pages, isRead) {
this.title = title;
this.author = author;
this.pages = pages;
this.isRead = isRead;
}
function addBookToLibrary() {
const title = document.querySelector("#title").value;
const author = document.querySelector("#author").value;
const pages = document.querySelector("#book-pages").value;
const isRead = document.querySelector("#status").checked;
const newBook = new Book(title, author, pages, isRead);
myLibrary.push(newBook);
console.log(myLibrary);
renderLibrary();
}
function renderLibrary() {
bookContainer.innerHTML = "";
for (let i = 0; i < myLibrary.length; i++) {
let book = myLibrary[i];
let card = document.createElement("div");
card.setAttribute("class", "book-card");
card.innerHTML = `
<p>Title: ${book.title}</p>
<p>Author: ${book.author}</p>
<p>Pages: ${book.pages}</p>
<p>Read: ${book.isRead ? "Yes" : "No"}</p>`;
bookContainer.appendChild(card);
}
}
newBook.addEventListener("click", () => {
dialogElement.showModal();
});
submit.addEventListener("click", (e) => {
e.preventDefault();
addBookToLibrary();
form.reset();
renderLibrary();
dialogElement.close();
});