-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (36 loc) · 1.18 KB
/
Copy pathscript.js
File metadata and controls
43 lines (36 loc) · 1.18 KB
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
const accessKey="8pk3UvgefWzLt7R3LOFznd5u_JMDJeJoy0i8mSRlS-A";
const searchForm=document.getElementById('search-form');
const searchBox=document.getElementById('search-box');
const searchResult=document.getElementById('search-result');
const showMoreBtn=document.getElementById('show-more-btn');
let keyword="";
let page=1;
async function searchImages(){
const url=`https://api.unsplash.com/search/photos?page=${page}&query=${keyword}&client_id=${accessKey}&per_page=12`;
const response = await fetch(url);
const data = await response.json();
const results = data.results;
if (page === 1) {
searchResult.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.target="_blank";
imagelink.appendChild(image);
searchResult.appendChild(imagelink);
})
showMoreBtn.style.display='block';
}
searchForm.addEventListener("submit",(e)=>{
e.preventDefault();
page=1;
keyword = searchBox.value;
searchImages();
})
showMoreBtn.addEventListener("click",()=>{
page++;
searchImages();
})