-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathImageFinder.js
44 lines (39 loc) · 1.22 KB
/
ImageFinder.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// All DOM variables
let container = document.querySelector('.container');
// Enter your Unsplash API key here
let key = '';
let btn = document.querySelector('#btn');
let input = document.querySelector('#query');
// function to retrieve photos from unsplash API
async function getPhotos(reqURL){
let res = await fetch(reqURL);
let data = await res.json();
console.log(data);
if(data.results.length){
for(let i = 0; i < data.results.length; i++){
let img = document.createElement('img');
img.src = data.results[i].links.download;
img.height = "200";
img.width = "200";
container.appendChild(img);
img.addEventListener('click',()=>{
alert(img.src);
})
}
}else{
alert('No images Found for this input!');
}
}
// init function
function init(){
let word = input.value;
input.value = "";
container.textContent = "";
let reqURL = `https://api.unsplash.com/search/photos?per_page=30&query=${word}&client_id=${key}`;
getPhotos(reqURL);
}
// Setting up all event listeners
btn.addEventListener('click', init);
document.addEventListener("keypress",e =>{
if(e.which === 13)init();
})