forked from Pranav-95/Random-Joke-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (26 loc) · 989 Bytes
/
script.js
File metadata and controls
29 lines (26 loc) · 989 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 jokeEl = document.getElementById("joke"); // div id = joke
const get_joke = document.getElementById("get_joke"); // button id = get_joke
const body = document.querySelector("body");
const toggle = document.querySelector(".toggle");
get_joke.addEventListener("click", generateJoke);
generateJoke();
async function generateJoke() {
// async makes a function return a Promise
// call the icanhaz API
var loading = document.getElementById("loading");
loading.style.visibility = "visible";
const jokeRes = await fetch("https://icanhazdadjoke.com/", {
// await makes a function wait for a Promise
headers: {
Accept: "application/json",
},
});
const joke = await jokeRes.json(); // save
loading.style.visibility = "hidden";
jokeEl.innerHTML = joke.joke;
}
toggle.addEventListener("click", () => {
body.classList.toggle("dark")
? (toggle.firstElementChild.className = "far fa-moon")
: (toggle.firstElementChild.className = "far fa-sun");
});