-
Notifications
You must be signed in to change notification settings - Fork 0
/
nasa.js
28 lines (18 loc) · 822 Bytes
/
nasa.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
27
document.querySelector('button').addEventListener('click',come); //this code is listening on button for any click
function come(){
let info= document.querySelector('input').value
console.log(info);
fetch(`https://api.nasa.gov/planetary/apod?api_key=WR3Ow1dlwaQUri3JTHmyHbyUUtGh13avME2Cgwf6&date=${info}`) //all these are extracted from their api
.then(res=>res.json())
.then(data=>{console.log(data)
document.querySelector('h2').innerText=data.title;
document.querySelector('p').innerText=data.explanation;
if(data.media_type=='image'){
document.querySelector('img').src=data.hdurl;
}
else if(data.media_type=='video'){
document.querySelector('iframe').src=data.url;
}
})
.catch(err=>console.log(err));
}