-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (48 loc) · 1.55 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
//selectors:
const formWrapper = document.querySelector(".form-wrapper");
const form = document.querySelector("#form");
const searchInput = document.querySelector("#searchInput");
const buttonWrapper = document.querySelector(".button-wrapper");
const searchButton = document.querySelector("#searchButton");
const clearButton = document.querySelector("#clearButton")
const imageListWrapper = document.querySelector(".imagelist-wrapper");
runEventListeners();
function runEventListeners(){
form.addEventListener("submit" , search);
clearButton.addEventListener("click" , clear);
searchInput.value ="";
}
function clear(){
searchInput.value ="";
imageListWrapper.innerHTML=""
}
function search(e){
const value = searchInput.value.trim();
fetch(`https://api.unsplash.com/search/photos?query=${value}` ,{
method : "GET" ,
headers : {
Authorization : "Client-ID Gp0thrm4628SAtqA_dXUoSVDSIhhdcueLxGuvSm0qbc"
}
})
.then((res)=> res.json())
.then((data)=> {
Array.from(data.results).forEach((image) =>{
addImageToUI(image.urls.small)
})
})
.catch((err)=> console.log(err));
e.preventDefault();
searchInput.value ="";
imageListWrapper.innerHTML=""
searchInput.focus();
}
function addImageToUI(url){
const div = document.createElement("div")
div.className = "card";
const img = document.createElement("img");
img.setAttribute("src" , url);
img.height= "400"
img.width="400"
div.append(img);
imageListWrapper.append(div)
}