-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (62 loc) · 1.67 KB
/
index.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
console.log("hello");
showbook();
let btn = document.getElementById('btn');
btn.addEventListener("click", function() {
let book = document.getElementById('book');
let author = document.getElementById("author");
let booklist = localStorage.getItem('booklist');
if (booklist == null) {
bookobj = [];
} else {
bookobj = JSON.parse(booklist);
}
let bookdetiles = {
name: book.value,
author: author.value,
date: Date()
};
bookobj.push(bookdetiles);
localStorage.setItem("booklist", JSON.stringify(bookobj));
console.log(bookobj);
book.value = "";
author.value = '';
showbook();
});
function showbook() {
let booklist = localStorage.getItem("booklist");
if (booklist == null) {
bookobj = [];
} else {
bookobj = JSON.parse(booklist);
}
let html = "";
bookobj.forEach(function(element, index) {
html += `<tr>
<th scope="row">${index +1}</th>
<td>${element.name}</td>
<td>${element.author}</td>
<td>${element.date}</td>
<td><button class="btn btn-primary" id="${index}" onclick= "deletebook(this.id)">Delete</button></td></tr>`;
});
let showbook = document.getElementById("showbook");
if (showbook.length != 0) {
showbook.innerHTML = html;
} else {
showbook.innerHTML = "you have not added any Book";
}
}
function deletebook(index) {
let booklist = localStorage.getItem("booklist");
if(booklist == null){
bookobj = [];
} else {
bookobj = JSON.parse(booklist);
}
bookobj.splice(index, 1);
localStorage.setItem("booklist", JSON.stringify(bookobj));
showbook();
}
let dlt = document.getElementById("dlt");
dlt.addEventListener("click", function(){
alert("don't worry I will delete all")
});