top-javascript-projects/library-app/js/javascript.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-12-30 15:44:02 -05:00
const myLibrary = [];
2025-01-04 08:18:56 -05:00
const container = document.getElementById("main");
2025-01-04 09:08:58 -05:00
const addBookBtn = document.getElementById("addBook");
const dialog = document.querySelector("dialog");
2024-12-30 15:44:02 -05:00
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read
// this.info = function() {
// return `${this.title} by ${this.author}, ${this.pages}, ${this.read}`
// }
}
function addBookToLibrary(title,author,pages,read) {
let newBook = new Book(title,author,pages,read)
myLibrary.push(newBook)
}
2025-01-04 08:18:56 -05:00
function createCard (book){
const card = document.createElement("div");
const classList = ["p-6", "border", "border-slate-300",
"bg-white", "rounded-xl", "shadow-xl", "flex", "flex-col", "gap-x-4"];
card.classList.add(...classList);
Object.keys(book).forEach((element) => {
if (element == 'title') {
let item = document.createElement('h2');
item.textContent = `${book[element]}`;
item.classList.add(...["font-bold", "text-2xl"])
card.appendChild(item);
} else {
let item = document.createElement('p');
item.textContent = `${element}: ${book[element]}`;
card.appendChild(item);
}
})
container.appendChild(card);
}
2025-01-04 09:08:58 -05:00
addBookBtn.addEventListener("click", () => {
dialog.showModal()
})
2025-01-04 08:18:56 -05:00
2024-12-30 15:44:02 -05:00
console.log(myLibrary)
addBookToLibrary("Test Book", "Kyle", "300", true)
2025-01-04 08:18:56 -05:00
console.log(myLibrary)
myLibrary.forEach((book) => createCard(book))