-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (29 loc) · 1.01 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
const adviceId = document.querySelector(".advice_id p span");
const adviceText = document.querySelector(".advice_text_container p q");
const button = document.querySelector(".dice_button");
async function fetchData() {
try {
// Fetch data using the Fetch API
const response = await fetch("https://api.adviceslip.com/advice");
// Check if the response is successful (status code 200)
if (!response.ok) {
throw new Error("Network response was not ok");
}
// Parse the JSON data in the response
const data = await response.json();
// Access the advice from the fetched data
adviceFromApi = data.slip.advice;
adviceIdFromApi = data.slip.id;
adviceId.innerHTML = adviceIdFromApi;
adviceText.innerHTML = adviceFromApi;
// Now you can use the advice in your application
} catch (error) {
adviceText.innerHTML = "❌ Something Went Wrong";
console.log(error);
}
}
function handleSubmit() {
fetchData();
}
button.addEventListener("click", handleSubmit);
fetchData();