-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (44 loc) · 1.35 KB
/
app.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
'use strict';
function setFocus(){
document.getElementById("text").focus();
}
function PressEnter(your_text, your_event) {
if(your_text != "" && your_event.keyCode == 13)
getWiki();
}
let searchBtn = document.getElementById("button");
searchBtn.addEventListener("click", getWiki);
let textSearch = document.getElementById("text");
function getWiki() {
event.preventDefault();
let rdyString = "";
let len = textSearch.value.length;
let queryString = textSearch.value;
for (var i = 0; i < len; i++) {
if (queryString[i] === " ") {
rdyString += "%20";
} else {
rdyString += queryString[i];
}
}
let api = 'https://en.wikipedia.org/w/api.php?action=opensearch&search='
+ rdyString
+ '&format=json&origin=*';
let myList = document.querySelector('ul');
while (myList.firstChild) {
myList.removeChild(myList.firstChild);
}
/* global fetch */
fetch(api)
.then(function(response) { return response.json(); })
.then(function(data) {
for (let i = 0; i < 10; i++) {
let listItem = document.createElement('li');
listItem.innerHTML = "<blockquote class='bg-info'><a href="
+ data[3][i] + "><p>"
+ data[1][i] + "</p><footer>"
+ data[2][i] + "</footer></a></blockquote>";
myList.appendChild(listItem);
}
});
}