-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
26 lines (24 loc) · 1001 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
document.addEventListener('DOMContentLoaded', () => {
const jokeElement = document.getElementById('joke');
// const jokeButton = document.getElementById('jokeButton');
const nextButton = document.getElementById('nextButton');
// Function to fetch and display a joke
function fetchJoke() {
fetch('https://v2.jokeapi.dev/joke/Any')
.then(response => response.json())
.then(data => {
if (data.joke) {
jokeElement.textContent = data.joke;
} else {
jokeElement.textContent = `${data.setup} - ${data.delivery}`;
}
})
.catch(error => {
console.error("Error in fetching", error);
jokeElement.textContent = "Oops! Something went wrong.";
});
}
// Event listeners for the buttons
// jokeButton.addEventListener('click', fetchJoke);
nextButton.addEventListener('click', fetchJoke);
});