-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (42 loc) · 1.48 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
document.addEventListener("DOMContentLoaded", function() {
// Set de trigger in the button
const UIgetJokes = document.querySelector('.get-jokes');
// Start function
UIgetJokes.addEventListener('click', getJokes);
function getJokes(e) {
e.target.style.pointerEvents = 'none';
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(xhr.status === 200) {
const response = JSON.parse(this.responseText);
let output = '';
if(response.type === 'success') {
response.value.forEach(function(message) {
output += `
<li class="jokeItem">
<p class="jokeItem__text">
"${message.joke}"
</p>
</li>
`;
});
UIgetJokes.style.pointerEvents = 'auto';
} else {
output += `<li>Something went wrong..</li>`
UIgetJokes.style.pointerEvents = 'auto';
}
document.querySelector('.jokes').innerHTML = output;
const modal = document.querySelector('.modalBg');
const closeModal = document.querySelector('.modalClose');
modal.classList.add('active');
closeModal.addEventListener('click', function(){
modal.classList.remove('active');
});
}
}
xhr.send();
e.preventDefault();
}
});