-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (85 loc) · 3.1 KB
/
app.js
File metadata and controls
86 lines (85 loc) · 3.1 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
// html elements
let bookId = 0;
const bookContainer = document.getElementById("all-books");
let book = document.createElement("li");
const title = document.getElementById("title");
const author = document.getElementById("author");
const primaryReadCheckbox = document.getElementById("primary-read");
const inputForm = document.getElementById("form");
// light/dark mode
let ModeToggleElement = document.querySelector("#toggle");
let lightModeToggle = false; // light mode by default, false returns dark mode
let darkModeToggle = true;
ModeToggleElement.addEventListener("click", () => {
if (darkModeToggle == true) {
lightModeToggle = true;
darkModeToggle = false;
document.body.style.backgroundColor = "black";
document.body.style.color = "white";
} else if (lightModeToggle == true) {
darkModeToggle = true;
lightModeToggle = false;
document.body.style.backgroundColor = "white";
document.body.style.color = "black";
}
});
// books code
const books = [];
function newBook() {
// created dom elements
let book = document.createElement("li");
let removeBtn = document.createElement("button");
let titleElement = document.createElement("span");
let authorElement = document.createElement("span");
let secondaryCheckBoxLabel = document.createElement("label");
let secondaryCheckBox = document.createElement("input");
// set created elements' properties
bookId++;
removeBtn.textContent = `Remove: ${title.value}, By: ${author.value}`;
titleElement.textContent = title.value;
authorElement.textContent = `By: ${author.value}`;
secondaryCheckBoxLabel.textContent = "Read:";
secondaryCheckBoxLabel.for = "Secondary-Read";
secondaryCheckBox.type = "checkbox";
secondaryCheckBox.name = "Secondary-Read";
secondaryCheckBox.classList.add("Secondary");
// append items creating book li
bookContainer.append(book);
book.append(removeBtn);
book.append(titleElement);
book.append(authorElement);
book.append(secondaryCheckBoxLabel);
book.append(secondaryCheckBox);
books.push({
title: title.value,
author: author.value,
readValue: "no",
// new book properties below if needed
});
console.log(books); // log books when added
let newBookId = bookId - 1;
console.log(books)
if (primaryReadCheckbox.checked === true) {
secondaryCheckBox.disabled = true;
secondaryCheckBox.checked = true;
books[newBookId].readValue = "yes";
}
secondaryCheckBox.addEventListener("click", () => {
secondaryCheckBox.disabled = true;
books[newBookId].readValue = "yes";
console.log(books); // update books when read value changed
});
removeBtn.addEventListener("click", () => {
book.remove(); // removes book from dom
books.splice(bookId); // removes from book object
})
};
inputForm.addEventListener("submit", (e) => {
e.preventDefault();
newBook();
// clean up
title.value = "";
author.value = "";
primaryReadCheckbox.checked = false;
// log books after cleanup
});