-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
129 lines (110 loc) · 3.62 KB
/
script.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// An Array for storing the book objects acts as a database..
let myLibrary = [];
// Book class
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
}
// Function to add a book to the library
function addBookToLibrary(title, author, pages, read) {
let book = new Book(title, author, pages, read);
myLibrary.push(book);
}
// DOM element selection
let mainContent = document.querySelector(".main-content");
let newBook = document.querySelector(".newBook");
let displayBtn = document.querySelector(".displayBtn");
let displayContainer = document.querySelector(".output-display");
// Event listener for the newBook button
newBook.addEventListener("click", () => {
let form = document.createElement("form");
form.classList = "data-form";
form.setAttribute("noValidate", true);
form.innerHTML = `
<label for="title">Title</label>
<input id="title" type="text" required />
<span id="titleError" class="error"></span>
<label for="author">Author</label>
<input id="author" type="text" required />
<span id="authorError" class="error"></span>
<label for="pages">Pages</label>
<input id="pages" type="number" required />
<span id="pagesError" class="error"></span>
<label for="read">Read</label>
<input id="read" type="text" required />
<span id="readError" class="error"></span>
<button type="submit" class="subBtn">Add Book</button>
`;
mainContent.append(form);
newBook.style.display = "none";
const submissionBtn = form.querySelector(".subBtn");
form.addEventListener("submit", (event) => {
event.preventDefault();
let valid = true;
const title = form.querySelector("#title");
const author = form.querySelector("#author");
const pages = form.querySelector("#pages");
const read = form.querySelector("#read");
if (!title.value) {
valid = false;
showError(title, "Title is required");
}
if (!author.value) {
valid = false;
showError(author, "Author Name is required");
}
if (!pages.value) {
valid = false;
showError(pages, "Pages are required");
}
if (!read.value) {
valid = false;
showError(read, "Read status is required");
}
if (valid) {
addBookToLibrary(title.value, author.value, pages.value, read.value);
newBook.style.display = "block";
mainContent.removeChild(form);
// Show the hidden displayBtn
displayBtn.style.display = "block";
}
});
function showError(input, message) {
const errorSpan = input.nextElementSibling;
errorSpan.textContent = message;
errorSpan.style.color = "red";
}
});
// Event listener for the display button
displayBtn.addEventListener("click", display);
// Function for displaying the books on the page
function display() {
myLibrary.forEach((item) => {
let displayCards = document.createElement("div");
displayCards.classList = "display-card";
createElements(displayCards, item);
displayContainer.appendChild(displayCards);
displayBtn.style.display = "none";
myLibrary = [];
});
}
// Helper function for creating elements
function createElements(container, e) {
container.innerHTML = `
<p>Book Name: ${e.title}</p>
<p>Written By: ${e.author}</p>
<p>Contains ${e.pages} Pages</p>
<p>Read? : ${e.read}</p>`;
let deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.classList.add("delete-btn");
container.appendChild(deleteBtn);
deleteBtn.addEventListener("click", () => {
container.remove();
displayBtn.style.display = "block";
});
}