Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (52 loc) · 1.53 KB
/
Copy pathscript.js
File metadata and controls
60 lines (52 loc) · 1.53 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
const section = document.getElementById('section');
const addButton = document.getElementById('add');
const inputTitle = document.getElementById('title');
const inputAuthor = document.getElementById('author');
const errorMsg = document.getElementsByClassName('errormsg');
const form = document.querySelector('form');
const box = JSON.parse(localStorage.getItem('box')) || [];
const renderBooks = () => {
let content = '';
box.forEach((element, index) => {
content += `<div index=${index}>
<ul class="list-of-books">
<li>${element.titleMsg}</li>
<li>${element.autorMsg}</li>
</ul>
<button class="romebtn" type="button" id=${index}>Remove</button>
<hr>
</div>`;
});
section.innerHTML = content;
const removeBooks = () => {
const removebtn = [...document.getElementsByClassName('romebtn')];
removebtn.forEach((item) => {
item.addEventListener('click', (elem) => {
box.splice(elem.target.id, 1);
localStorage.setItem('box', JSON.stringify(box));
renderBooks();
});
});
};
removeBooks();
};
const addBooks = () => {
addButton.addEventListener('click', () => {
const titleMsg = inputTitle.value;
const autorMsg = inputAuthor.value;
if (titleMsg && autorMsg) {
const book = {
titleMsg,
autorMsg,
};
box.push(book);
localStorage.setItem('box', JSON.stringify(box));
renderBooks();
form.reset();
} else {
errorMsg.innerHTML = 'input title and author';
}
});
};
renderBooks();
addBooks();