-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
37 lines (27 loc) · 835 Bytes
/
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
document.querySelector('.get-jokes').addEventListener('click', getJokes);
function getJokes(e) {
e.preventDefault()
const number = document.querySelector('#number').value;
const xhr = new XMLHttpRequest();
xhr.open('GET', `https://api.icndb.com/jokes/random/${number}`, true);
xhr.onload = function() {
if(this.status === 200) {
const response = JSON.parse(this.responseText);
console.log(response)
let output = '';
if(response.type === 'success') {
response.value.forEach((joke)=> {
output +=
`
<li>${joke.joke}</li>
`
})
} else {
output += '<li>Something went wrong.</li>'
}
document.querySelector('.jokes').innerHTML = output;
}
}
xhr.send()
document.querySelector('#number').value = '';
}