-
Notifications
You must be signed in to change notification settings - Fork 0
/
WikipediaSearch.js
89 lines (65 loc) · 2.26 KB
/
WikipediaSearch.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
77
78
79
80
81
82
83
84
85
86
87
88
89
let searchInputElem = document.getElementById('searchInput');
let searchResultsEl = document.getElementById('searchResults');
let spinnerEl = document.getElementById('spinner');
function createAndAppendSearchResult(result) {
let{title,link,description} = result;
//Creating Result Item Div Container -- result-item
let resultItemEl = document.createElement('div');
resultItemEl.classList.add('result-item');
searchResultsEl.appendChild(resultItemEl);
//Creating Title Elem Anchor elem -- result-title
let resultTitleEl = document.createElement('a');
resultTitleEl.classList.add('result-title');
resultTitleEl.textContent = title;
resultTitleEl.href = link;
resultTitleEl.target = '_blank';
resultItemEl.appendChild(resultTitleEl);
//Creating Break Elem
let titleBreakEl = document.createElement('br');
resultItemEl.appendChild(titleBreakEl);
//Creating URL Elem Anchor elem -- result-url
let urlEl = document.createElement('a');
urlEl.classList.add('result-url');
urlEl.href = link;
urlEl.target = "_blank";
urlEl.textContent = link;
resultItemEl.appendChild(urlEl);
//Creating Break Elem
let linkBreakEl = document.createElement('br');
resultItemEl.appendChild(linkBreakEl);
//Creating Description Elem -- line-description
let descriptionEl = document.createElement('p');
descriptionEl.classList.add('line-description');
descriptionEl.textContent = description;
resultItemEl.appendChild(descriptionEl);
}
function displayResults(searchResults){
spinnerEl.classList.toggle('d-none');
for(let result of searchResults){
createAndAppendSearchResult(result);
}
}
function searchWikipedia(event) {
if(event.key === 'Enter'){
spinnerEl.classList.toggle('d-none');
//Whenever user enters 'Enter' This means he doing new search
//So make searchResultEl empty
searchResultsEl.textContent = '';
let searchInput = searchInputElem.value;
//console.log(searchInput);
let url = "https://apis.ccbp.in/wiki-search?search=" + searchInput;
//Request Configuration
let options = {
method: 'GET',
}
fetch(url,options)
.then(function(response){
return response.json();
})
.then(function(jsonData){
let {search_results} = jsonData;
displayResults(search_results);
})
}
}
searchInputElem.addEventListener('keydown',searchWikipedia);