-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (65 loc) · 1.82 KB
/
Copy pathscript.js
File metadata and controls
79 lines (65 loc) · 1.82 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
let input = document.getElementById("searchInput");
let con = document.getElementById("searchResults");
let url = "https://apis.ccbp.in/book-store";
let spinnerEl = document.getElementById("spinner");
let booklist;
function createAndAppend(book) {
let {
imageLink,
author
} = book;
let bookcon = document.createElement("div");
let img = document.createElement("img");
img.src = imageLink;
img.classList.add("image");
bookcon.appendChild(img);
let para = document.createElement("p");
para.textContent = author;
para.classList.add("para");
bookcon.appendChild(para);
bookcon.classList.add("col-6");
con.appendChild(bookcon);
}
function display(results) {
for (let i of results) {
createAndAppend(i);
}
}
let options = {
method: "GET"
};
fetch(url, options)
.then(function(response) {
return response.json();
})
.then(function(data) {
booklist = data;
let {
search_results
} = data;
display(search_results);
});
function searchBook(event) {
if (event.key === "Enter") {
spinnerEl.classList.remove("d-none");
con.textContent = "";
let inp = input.value;
let url1 = "https://apis.ccbp.in/book-store?title=" + inp;
console.log(url1);
let options = {
method: "GET"
};
fetch(url1, options)
.then(function(response) {
return response.json();
})
.then(function(data) {
booklist = data;
let {
search_results
} = data;
display(search_results);
});
}
}
input.addEventListener("keydown", searchBook);