-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathapp.js
More file actions
29 lines (24 loc) · 819 Bytes
/
app.js
File metadata and controls
29 lines (24 loc) · 819 Bytes
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
const image = document.querySelector('img');
const jokeDIV = document.querySelector('#display-joke');
const button = document.querySelector('#get-joke');
button.addEventListener('click', function(){
getRandomJoke();
})
function getRandomJoke(){
const ajax = new XMLHttpRequest;
const url = 'https://api.chucknorris.io/jokes/random'
ajax.open('GET', url, true);
ajax.onreadystatechange = function(){
if(this.status === 200 && this.readyState === 4){
console.log(this.responseText);
let data = JSON.parse(this.responseText);
jokeDIV.innerHTML = `${data.value}`
} else {
this.onerror = onerror();
}
}
ajax.send();
}
function onerror(){
jokeDIV.textContent = 'There was an error!';
}