-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (45 loc) · 1.66 KB
/
Copy pathscript.js
File metadata and controls
61 lines (45 loc) · 1.66 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const apiKey = "x_jkLflL3MqmcgXcTuEsj0EXpvVATIR8bXH0pFtmLlw";
const formElement = document.querySelector('form');
const input = document.getElementById('searchInput');
const searchResultContainer = document.querySelector('.searchResultContainer');
const showMore = document.getElementById('showMore');
let inputData = "";
let page = 1;
async function searchImages() {
inputData = input.value;
const url = `https://api.unsplash.com/search/photos?page=${page}&query=${inputData}&client_id=${apiKey}`;
const response = await fetch(url);
// console.log(response);
const data = await response.json();
console.log(data);
const results = data.results
if(page === 1){
searchResultContainer.innerHTML = "";
}
results.map((result) =>{
const imageWrapper = document.createElement('div');
imageWrapper.classList.add('searchResult');
const image = document.createElement('img');
image.src = result.urls.small;
image.alt = result.alt_description;
const imageLink = document.createElement('a');
imageLink.href = result.links.html;
imageLink.target = "_blank";
imageLink.textContent = result.alt_description;
imageWrapper.appendChild(image);
imageWrapper.appendChild(imageLink);
searchResultContainer.appendChild(imageWrapper);
});
page++;
if(page > 1){
showMore.style.display = "block"
}
}
formElement.addEventListener('submit', (event) =>{
event.preventDefault();
page = 1;
searchImages();
});
showMore.addEventListener('click', () =>{
searchImages();
});