-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (33 loc) · 1.16 KB
/
script.js
File metadata and controls
40 lines (33 loc) · 1.16 KB
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
const url = "https://api.api-ninjas.com/v1/jokes?limit=1";
const jokeOutput = document.getElementById("joke-output");
const loading = document.getElementById("loading");
const jokeBtn = document.getElementById("jokeBtn");
const shareBtn = document.getElementById("shareBtn");
const themeBtn = document.getElementsByClassName("theme-controller")[0];
const fetchJoke = async () => {
loading.classList.remove("hidden");
jokeOutput.classList.add("hidden")
try {
const response = await fetch(url, {
method: "GET",
headers: {
"X-Api-Key": API_KEY,
}
});
const data = await response.json();
// Display the fetched joke in a creative way
jokeOutput.innerHTML = `<p class="text-success text-xl">"${data[0].joke}"</p>`;
} catch (error) {
// Handle errors, display an error message
jokeOutput.innerHTML = `<p class="text-xl font-semibold text-warning">Oops! Something went wrong.</p>`;
}
finally {
loading.classList.add("hidden");
jokeOutput.classList.remove("hidden");
}
};
fetchJoke();
jokeBtn.addEventListener("click", fetchJoke);
shareBtn.addEventListener("click", () => {
alert("Sharing the joke!"); //
});