-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
91 lines (74 loc) · 2.53 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
function showContent(sectionId) {
const sections = document.getElementsByClassName('content');
for (let i = 0; i < sections.length; i += 1) {
sections[i].style.display = 'none'; // Hide all content sections
}
const selectedSection = document.getElementById(sectionId);
selectedSection.style.display = 'block'; // Show the selected content section
}
document.addEventListener('DOMContentLoaded', () => {
showContent('books'); // Show the initial content section
const time = new Date();
// Create a <p> element for the date and time
const dateElement = document.getElementById('currentDate');
dateElement.textContent = time;
});
class BookManager {
constructor() {
this.books = JSON.parse(localStorage.getItem('books')) || [];
this.booksDiv = document.getElementById('books');
this.addBookForm = document.getElementById('addBookForm');
this.titleInput = document.getElementById('titleInput');
this.authorInput = document.getElementById('authorInput');
this.displayBooks();
this.addEventListeners();
}
displayBooks() {
this.booksDiv.innerHTML = '';
if (this.books.length === 0) {
this.booksDiv.textContent = 'No books in the collection.';
return;
}
const removeBook = (index) => {
this.books.splice(index, 1);
localStorage.setItem('books', JSON.stringify(this.books));
this.displayBooks();
};
this.books.forEach((book, index) => {
this.booksDiv.innerHTML += `<h2>All Awesome Books</h2>
<div class="book">
<span class="book-title">${book.title}</span>
<span class="book-author">by ${book.author}</span>
<button class="remove-button" type="button" data-index="${index}">Remove</button>
</div>`;
});
const removeButtons = document.getElementsByClassName('remove-button');
Array.from(removeButtons).forEach((button, index) => {
button.addEventListener('click', () => {
removeBook(index);
});
});
}
addBook(title, author) {
const book = {
title,
author,
};
this.books.push(book);
localStorage.setItem('books', JSON.stringify(this.books));
this.displayBooks();
}
addEventListeners() {
this.addBookForm.addEventListener('submit', (event) => {
event.preventDefault();
this.addBook(this.titleInput.value, this.authorInput.value);
this.titleInput.value = '';
this.authorInput.value = '';
});
}
static initialize() {
const bookManager = new BookManager();
return bookManager;
}
}
BookManager.initialize();