-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
24 lines (21 loc) · 807 Bytes
/
script.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
const contentElement = document.getElementById("content");
const generateButton = document.getElementById("generateButton");
const slipid = document.getElementById("adviceid")
// Function to fetch and display a new advice
function fetchNewAdvice() {
fetch("https://api.adviceslip.com/advice")
.then((response) => response.json())
.then((data) => {
const advice = data.slip.advice;
contentElement.textContent = "\"" + advice + "\"";
const id = data.slip.id;
slipid.textContent = "advice #" + id
})
.catch((error) => {
console.log("Error fetching data:", error);
});
}
// Display initial advice on page load
window.addEventListener("load", fetchNewAdvice);
// Event listener for the button click
generateButton.addEventListener("click", fetchNewAdvice);