-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
26 lines (23 loc) · 732 Bytes
/
script.js
File metadata and controls
26 lines (23 loc) · 732 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
// Variables
const dice = document.getElementById("dice");
const adviceNum = document.getElementById("advice-number");
const adviceText = document.querySelector(".advice-text");
// Run the showQuote function when the page is loaded
window.onload = showQuote;
// Eventlistener for dice button
dice.addEventListener("click", function(){
showQuote();
});
// showQuote function to show random quote from API
function showQuote(){
fetch("https://api.adviceslip.com/advice")
.then(response => response.json())
.then((data) => data.slip)
.then((data) => {
adviceNum.textContent = data.id;
adviceText.textContent = data.advice;
})
.catch((error) => {
alert(`Error ${error}`);
});
}