-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
144 lines (120 loc) · 4.71 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const promptText = document.getElementById('prompt');
const myLibrary = [{
title: 'A Game of Thrones',
author: 'George R. R. Martin',
pages: 694,
read: 'Yes',
}];
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.id = `${title}_${Date.now()}`;
}
function addBookToLibrary() {
if (title.value === '' || author.value === '' || pages.value === '') {
promptText.textContent = 'Please fill out all of the book details';
return false;
}
if (title.value !== '' && author.value !== '' && pages.value !== '') {
title = document.getElementById('title').value;
author = document.getElementById('author').value;
pages = document.getElementById('pages').value;
read = document.getElementById('book_read').value;
const newBook = new Book(title, author, pages, read);
myLibrary.push(newBook);
}
drawTable();
}
addBookToLibrary.prototype = Object.create(Book.prototype);
document.getElementById('addBook');
addBook.addEventListener('click', (addBookToLibrary));
addBook.addEventListener('click', () => {
if (title.value !== '' && author.value !== '' && pages.value !== '') {
document.getElementById('title').value = '';
document.getElementById('author').value = '';
document.getElementById('pages').value = '';
promptText.textContent = '';
}
});
function drawTable() {
myLibrary.forEach((library) => {
const tbl = document.createElement('table');
const tblHead = document.createElement('thead');
const tblBody = document.createElement('tbody');
const firstRow = document.createElement('tr');
const secondRow = document.createElement('tr');
const secondCells = document.createElement('td');
const secondCellsone = document.createElement('td');
const secondCellstwo = document.createElement('td');
const secondCellsthree = document.createElement('td');
const secondCellsfour = document.createElement('td');
const secondCellsfive = document.createElement('td');
const tableHeaders = document.createElement('th');
const tableHeadersone = document.createElement('th');
const tableHeaderstwo = document.createElement('th');
const tableHeadersthree = document.createElement('th');
const cellText = document.createTextNode(library.title);
const authorText = document.createTextNode(library.author);
const pagesText = document.createTextNode(library.pages);
const textread = document.createTextNode(library.read);
const headerText = document.createTextNode('Title');
const headerTextone = document.createTextNode('Author');
const headerTexttwo = document.createTextNode('Pages');
const headerTextthree = document.createTextNode('Read');
firstRow.appendChild(tableHeaders);
firstRow.appendChild(tableHeadersone);
firstRow.appendChild(tableHeaderstwo);
firstRow.appendChild(tableHeadersthree);
tableHeaders.appendChild(headerText);
tableHeadersone.appendChild(headerTextone);
tableHeaderstwo.appendChild(headerTexttwo);
tableHeadersthree.appendChild(headerTextthree);
secondCells.appendChild(cellText);
secondCellsone.appendChild(authorText);
secondCellstwo.appendChild(pagesText);
secondCellsthree.appendChild(textread);
secondRow.appendChild(secondCells);
secondRow.appendChild(secondCellsone);
secondRow.appendChild(secondCellstwo);
secondRow.appendChild(secondCellsthree);
secondRow.appendChild(secondCellsfour);
secondRow.appendChild(secondCellsfive);
const readButton = document.createElement('button');
const readText = document.createTextNode('Change read status');
const deleteButton = document.createElement('button');
const deleteText = document.createTextNode('Delete');
secondCellsfour.appendChild(readButton);
readButton.appendChild(readText);
secondCellsfive.appendChild(deleteButton);
deleteButton.appendChild(deleteText);
const idData = document.createElement('td');
idData.textContent = `${Date.now()}`;
tblHead.appendChild(firstRow);
tblBody.appendChild(secondRow);
tbl.appendChild(tblHead);
tbl.appendChild(tblBody);
document.body.appendChild(tbl);
deleteButton.addEventListener('click', (book) => {
if (book.id === Object.id) {
tbl.remove();
}
});
readButton.addEventListener('click', () => {
if (textread.textContent === 'Yes') {
textread.textContent = 'No';
} else if (textread.textContent === 'No') {
textread.textContent = 'Yes';
}
});
});
myLibrary.length = 0;
}
function openForm() {
document.getElementById('myForm').style.display = 'block';
}
function closeForm() {
document.getElementById('myForm').style.display = 'none';
}
drawTable();