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