-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
24 lines (22 loc) · 879 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
document.querySelector('button').addEventListener('click',getFetch)
function getFetch(){
const choice = document.querySelector('input').value
const url = `https://api.nasa.gov/planetary/apod?api_key=vIn5A36tyJL0QLmQG6ahSCP89ei7GYOtTa1XGD3U&date=${choice}`
fetch(url)
.then(res => res.json())
.then(data => {
console.log(data)
if (data.media_type === 'image'){
document.querySelector('img').src = data.hdurl
document.querySelector('iframe').style.display = 'none'
} else if (data.media_type === 'video'){
document.querySelector('iframe').src = data.url
document.querySelector('img').style.display = 'none'
}
document.querySelector('h2').innerText = data.title
document.querySelector('p').innerText = data.explanation
})
.catch(err => {
console.log(`error ${err}`)
})
}