-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
32 lines (26 loc) · 1.02 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
// const axios = require('axios').default;
// axios.<method> will now provide autocomplete and parameter typings
//Requesting for a random Joke from API and returning the recieved Joke data
const getTheJoke = async () => {
const response = await axios.get("https://official-joke-api.appspot.com/jokes/random");
return response.data;
}
//Listening for specific button click event
window.addEventListener("click", (evt) => {
if (evt.target.id === "GetJoke") {
addJoke();
}
})
//Adding Joke to the page on button click event
const addJoke = async () => {
const Joke = await getTheJoke();
const bodycontainer = document.querySelector(".body-container");
//Clearing previous Joke on page (if any) before adding the new one
bodycontainer.innerHTML = "";
const setup = document.createElement("p");
setup.append(`${Joke.setup}`);
const punchline = document.createElement("p");
punchline.append(`- ${Joke.punchline}`);
bodycontainer.append(setup);
bodycontainer.append(punchline);
}