-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
45 lines (35 loc) · 1.38 KB
/
script.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
// API: https://github.com/statistron/wikistats
// Random quotes API
document.addEventListener("DOMContentLoaded", () => {
const button = document.querySelector("button");
async function refreshQuote() {
var url = "https://api.statistron.xyz/v1/wikistats?q=random&limit=5";
//change value of limit parameter as needed
//max limit value is 50 quotes per request
fetch(url)
.then((response) => response.json())
.then((jsonstat) => {
console.log(jsonstat);
//Check if div quote exists, if it does, remove it to display the next set of quotes
var isdivquote = document.getElementsByClassName("div-quote");
if (isdivquote.length > 0) {
document.querySelectorAll(".div-quote").forEach((el) => el.remove());
}
// loop through the objects in the response array
for (var i = 0; i < jsonstat.length; i += 1) {
var div = document.createElement("div");
div.className = "div-quote";
div.innerHTML =
'<figure class="quote"><blockquote>' +
jsonstat[i].quote +
"</blockquote><figcaption> —<cite>" +
jsonstat[i].source +
"</cite> </figcaption></figure> <br>";
document.body.appendChild(div);
}
});
}
button.addEventListener("click", refreshQuote);
// call refreshQuote
refreshQuote();
});