-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
76 lines (67 loc) · 2.07 KB
/
main.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
// variables
const inputTitle = document.querySelector(".input-title");
const inputAuthor = document.querySelector(".input-author");
const inputIsbn = document.querySelector(".input-isbn");
const btnSumit = document.querySelector(".btn-submit");
const perfect = document.querySelector(".perfect");
const btnRemove = document.querySelector(".last");
const btnClear = document.querySelector(".clear");
// starting coding
btnSumit.addEventListener("click", function() {
const title = inputTitle.value;
const author = inputAuthor.value;
if (!Number(title) && title != "" && !Number(author)) {
check();
display();
}
});
function check() {
let getLocalStorage = window.localStorage.getItem("book");
const title = inputTitle.value;
const author = inputAuthor.value;
const isbn = inputIsbn.value;
const obj = {
title,
author,
isbn,
};
if (getLocalStorage == null) {
listItems = [];
} else {
listItems = JSON.parse(getLocalStorage);
}
listItems.push(obj);
window.localStorage.setItem("book", JSON.stringify(listItems));
}
display();
function display() {
let getLocalStorage = window.localStorage.getItem("book");
if (getLocalStorage == null) {
listItems = [];
} else {
listItems = JSON.parse(getLocalStorage);
}
let displays = ``;
for (const [i, n] of listItems.entries()) {
displays += `<div class="list-book">
<p>${n.title}</p>
<p>${n.author}</p>
<p>${n.isbn}</p>
<p class="last" onclick="removeOne(${i})"><i class="fas fa-times"></i></button>
</div>`;
}
perfect.innerHTML = displays;
inputTitle.value = "";
inputAuthor.value = "";
inputIsbn.value = "";
}
function removeOne(index) {
let getLocalStorage = window.localStorage.getItem("book");
listItems = JSON.parse(getLocalStorage);
listItems.splice(index, 1);
window.localStorage.setItem("book", JSON.stringify(listItems));
display();
}
btnClear.addEventListener("dblclick", function() {
window.localStorage.clear();
});