-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
26 lines (20 loc) · 942 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
25
26
let searchButton = document.querySelector("#search")
//Add an event listener to the button that runs the function sendApiRequest when it is clicked
searchButton.addEventListener("click", ()=>{
console.log("button pressed")
sendApiRequest()
})
//An asynchronous function to fetch data from the API.
async function sendApiRequest(){
let API_KEY="tET5zGRbZ5fAd04rKGd1IH2JTr8TXhz4hzUiZBpO"
let response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=${API_KEY}`);
console.log(response)
let data = await response.json()
console.log(data)
useApiData(data)
}
//function that does something with the data received from the API. The name of the function should be customized to whatever you are doing with the data
function useApiData(data){
document.querySelector("#content").innerHTML += data.explanation
document.querySelector("#content").innerHTML += `<img src="${data.url}">`
}