-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
23 lines (21 loc) · 818 Bytes
/
main.js
File metadata and controls
23 lines (21 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Example fetch using pokemonapi.co
document.querySelector('button').addEventListener('click', getFetch)
function getFetch(){
const choice = document.querySelector('input').value
const url = 'https://api.nasa.gov/planetary/apod?api_key=86d5qJifycnTLmGzhHWsLQMB6Cs3teaBarBgF7WH'
fetch(url)
.then(res => res.json()) // parse response as JSON
.then(data => {
console.log(data)
if(data.media_type === "image"){
document.querySelector('img').src= data.hdurl
}else if(data.media_type === 'video'){
document.querySelector('iframe').src=data.url
}
document.querySelector('h3').innerText = data.explanation
document.querySelector('h2').innerText = data.date
})
.catch(err => {
console.log(`error ${err}`)
});
}