-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (59 loc) · 1.85 KB
/
index.js
File metadata and controls
71 lines (59 loc) · 1.85 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
59
60
61
62
63
64
65
66
67
68
69
70
71
let form = document.querySelector("form");
let bookListRoot = document.querySelector(".book_list");
const nameElm = form.elements.bookName;
const authorElm = form.elements.bookAuthor;
const imageELm = form.elements.bookImage;
class Book{
constructor(name, author, img){
this.name = name;
this.author = author;
this.img = img;
this.isRead = false;
}
toggleIsRead(){
this.isRead = !this.isRead;
}
}
class BookList{
constructor(books = []){
this.books = books;
}
addBook(name, author, img){
let book = new Book(name, author, img);
this.books.push(book);
this.createUi();
}
createUi(){
bookListRoot.innerHTML = "";
this.books.forEach((book) => {
let li = document.createElement("li");
let h1 = document.createElement("h1");
h1.innerText = book.name;
let p = document.createElement("p");
p.innerText = book.author;
let img = document.createElement("img");
img.src = book.img;
let button = document.createElement("button");
button.innerText = book.isRead ? "completed" : "Mark as Read";
button.classList.add("card_button");
button.addEventListener("click", () => {
book.toggleIsRead();
this.createUi();
});
li.append(img, h1, p, button);
bookListRoot.append(li);
});
}
}
let library = new BookList();
function handleSubmit(event) {
event.preventDefault();
const name = nameElm.value;
const author = authorElm.value;
const img = imageELm.value;
library.addBook(name, author, img);
nameElm.value = "";
authorElm.value = "";
imageELm.value = "";
}
form.addEventListener("submit", handleSubmit);