-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (50 loc) · 1.66 KB
/
script.js
File metadata and controls
58 lines (50 loc) · 1.66 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
53
54
55
56
57
58
const addbookbtn=document.querySelector("#button");
const formContainer=document.querySelector("#formContainer");
const libraryContainer = document.querySelector("#libraryContainer");
const newform = document.querySelector("#newform");
let myLibrary = [];
addbookbtn.addEventListener("click",()=>{
formContainer.style.display =
formContainer.style.display === "none" ? "block" : "none";
});
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
}
newform.addEventListener("submit",function(event){
event.preventDefault();
const title=document.querySelector("#title").value;
const author=document.querySelector("#author").value;
const pages=document.querySelector("#pages").value;
const read=document.querySelector("#read").checked;
const newBook=new Book(title,author,pages,read);
myLibrary.push(newBook);
displayBooks();
newform.reset();
formContainer.style.display = "none";
});
function displayBooks(){
libraryContainer.innerHTML="";
for(let i=0;i<myLibrary.length;i++)
{
let bookDiv=document.createElement("div");
bookDiv.classList.add("book");
bookDiv.innerHTML = `
<h3>${myLibrary[i].title}</h3>
<p>Author: ${myLibrary[i].author}</p>
<p>Pages: ${myLibrary[i].pages}</p>
<p>Read: ${myLibrary[i].read ? "Yes" : "No"}</p>`;
let removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.addEventListener("click", function () {
myLibrary.splice(i, 1);
displayBooks();
});
bookDiv.appendChild(removeBtn);
libraryContainer.appendChild(bookDiv);
}
}