-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjs.js
58 lines (35 loc) · 1.33 KB
/
js.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
58
const acceskey = "Enter your api key"
const accessurl = "https://api.unsplash.com/photos/?page="
const serachinput = document.getElementById("Search");
const searchbtn = document.getElementById("btn");
const resultSection = document.querySelector(".imagesec");
const FormEL = document.querySelector("form");
const showMoreBtn = document.getElementById("showMore");
let page = 1;
async function getImages() {
const keyword = serachinput.value;
const response = await fetch(`https://api.unsplash.com/search/photos?page=${page}&query=${keyword}&client_id=${acceskey}&per_page=12`);
const data = await response.json();
console.log(data);
const results = data.results;
if (page === 1) {
resultSection.innerHTML = "";
}
results.map((result) => {
const image = document.createElement("img");
image.src = result.urls.small;
const imageLink = document.createElement("a");
imageLink.href = result.links.html;
imageLink.appendChild(image);
resultSection.appendChild(imageLink);
})
showMoreBtn.style.display = "block";
}
FormEL.addEventListener("submit", (event) => {
event.preventDefault();
getImages();
})
showMoreBtn.addEventListener("click", (e) => {
page++;
getImages();
})