-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (34 loc) · 1.12 KB
/
script.js
File metadata and controls
40 lines (34 loc) · 1.12 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
function onClick(e) {
e.preventDefault();
let s = document.getElementById('selector');
let type = s.options[s.selectedIndex].value;
// setup URL
let url = "https://official-joke-api.appspot.com/jokes/" + type + "/random";
// call API
fetch(url)
.then(function(response) {
// make sure the request was successful
if (response.status != 200) {
return {
text: "Error calling the joke API service: " + response.statusText
}
}
return response.json();
}).then(function(json) {
// update DOM with response
updateResult(json[0].setup);
updatePunchline(json[0].punchline);
});
}
function updateResult(info) {
document.getElementById('joke').textContent = info;
}
function updatePunchline(info) {
document.getElementById('punchline').style.visibility = "hidden";
document.getElementById('punchline').textContent = info;
}
function show() {
document.getElementById('punchline').style.visibility = "visible";
}
document.getElementById('getJoke').addEventListener('click', onClick);
document.getElementById('getPunchline').addEventListener('click', show);