-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
111 lines (94 loc) · 3.61 KB
/
main.js
File metadata and controls
111 lines (94 loc) · 3.61 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
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
let myLibrary = [];
class Book {
constructor (name, author, status) {
this.name = name;
this.status = status;
this.author = author;
}
}
//default books for displaying tables
let sapiens=new Book("Sapiens","Yuval Noah Harari","read");
let naruto=new Book("Naruto","Masashi Kishimoto","not read");
let dues=new Book("Dues", "Yuval Noah Harari", "read");
//push the default books to library
myLibrary.push(sapiens);
myLibrary.push(naruto);
myLibrary.push(dues);
function addBookToLibrary() {
//create a new book and push it to the library.
const newBook = new Book(bookName.value, authorName.value, bookStatus.value);
let exists=0;
for (element in myLibrary)
{
//check if the newbook is already existing in the library
if(newBook.name == myLibrary[element].name) {
//if book already exists warn user about the same.
alert("book already exists");
exists=1;
return;
}
}
//if book doesnt exist then push it
if (exists==0){
myLibrary.push(newBook);
}
return;
}
let submitButton = document.getElementById("submitButton");
let bookName = document.getElementById("bookName");
let authorName = document.getElementById("authorName");
let bookStatus = document.getElementById("status");
let bookForm = document.querySelector("form");
let tableBody = document.getElementById("tableBody");
bookForm.addEventListener("submit", (e)=>{
e.preventDefault();
addBookToLibrary();
displayOutput();
})
function displayOutput(){
tableBody.innerHTML="";
myLibrary.forEach((element,index) => {
tempRow = tableBody.insertRow(index);
let cell1= tempRow.insertCell(0);
let cell2= tempRow.insertCell(1);
let cell3= tempRow.insertCell(2);
let cell4 = tempRow.insertCell(3);
cell1.innerHTML = element.name;
cell2.innerHTML = element.author;
// creating button element for status
if(element.status == "not read") {
cell3.innerHTML= `<button class="statusButton" data-status="not read" data-index="${index}">not read</button>`;
}
else {
cell3.innerHTML= `<button class="statusButton" data-status="read" data-index="${index}">read</button>`;
}
cell4.innerHTML= `<button class="deleteButton" data-index="${index}">Delete</button>`;
});
}
tableBody.addEventListener("click", (e) => {
// check if the clicked element is a statusButton
if (e.target.classList.contains("statusButton")) {
// get the index of the book from the data-index attribute
let index = e.target.getAttribute("data-index");
// toggle the status of the book in the myLibrary array
myLibrary[index].status =
myLibrary[index].status === "not read" ? "read" : "not read";
// update the data-status attribute of the button
e.target.setAttribute("data-status", myLibrary[index].status);
// update the text content of the button
e.target.textContent = myLibrary[index].status;
displayOutput();
}
if (e.target.classList.contains("deleteButton")) {
// get the index of the book from the data-index attribute
let index = e.target.getAttribute("data-index");
// toggle the status of the book in the myLibrary array
myLibrary.splice(index,1) ;
// // update the data-status attribute of the button
// e.target.setAttribute("data-status", myLibrary[index].status);
// // update the text content of the button
// e.target.textContent = myLibrary[index].status;
displayOutput();
}
});
displayOutput();