-
Notifications
You must be signed in to change notification settings - Fork 0
/
app3.js
38 lines (24 loc) · 907 Bytes
/
app3.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
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', `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(function(joke){
output += `<li>${joke.joke}</li>`;
})
} else {
output += '<li>Something went wrong</li>';
}
document.querySelector('.jokes').innerHTML = output;
}
}
xhr.send();
// console.log(number);
e.preventDefault();
}