-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.93 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
let searchInputEl = document.getElementById("searchInput");
let searchResultsEl = document.getElementById("searchResults");
let spinnerEl = document.getElementById("spinner");
function createAndAppendResults(searchData) {
let { title, link, description } = searchData;
let resultItemEl = document.createElement("div");
resultItemEl.classList.add("result-item", "shadow-sm");
searchResultsEl.appendChild(resultItemEl);
let titleEl = document.createElement("a");
titleEl.href = link;
titleEl.target = "_blank";
titleEl.textContent = title;
titleEl.classList.add("result-title");
resultItemEl.appendChild(titleEl);
let titleBreakEl = document.createElement("br");
resultItemEl.appendChild(titleBreakEl);
let urlEl = document.createElement("a");
urlEl.textContent = link;
urlEl.href = link;
urlEl.classList.add("result-url");
urlEl.target = "_blank";
resultItemEl.appendChild(urlEl);
let lineBreakEl = document.createElement("br");
resultItemEl.appendChild(lineBreakEl);
let descriptionEl = document.createElement("p");
descriptionEl.textContent = description;
descriptionEl.classList.add("link-description");
resultItemEl.appendChild(descriptionEl);
}
function displayResults(results) {
spinnerEl.classList.toggle("d-none");
for (let searchData of results) {
createAndAppendResults(searchData);
}
}
function wikipediaSearch(event) {
if (event.key === "Enter") {
spinnerEl.classList.toggle("d-none");
searchResultsEl.textContent = "";
let searchInputElValue = searchInputEl.value;
let url = "https://apis.ccbp.in/wiki-search?search=" + searchInputElValue;
let options = {
method: "GET",
};
fetch(url, options)
.then(function (response) {
return response.json();
})
.then(function (jsonData) {
let { search_results } = jsonData;
displayResults(search_results);
});
}
}
searchInputEl.addEventListener("keydown", wikipediaSearch);