-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (35 loc) · 1.36 KB
/
Copy pathscript.js
File metadata and controls
46 lines (35 loc) · 1.36 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
function SearchImages(){
let accessKey = "k1sdfis1JxoP_jg1wr2zEhJs-aN6JqUzcecLoMAyTwI";
let query = document.getElementById("search").value;
let url = "https://api.unsplash.com/search/photos?client_id="+accessKey+"&query="+query;
//let url = "https://api.unsplash.com/search/photos?page=1&query=office";
//API request
fetch(url)
.then(function(data){
return data.json();
})
// appending pictures recieved to the HTML file
.then(function(data){
console.log(data);
const myDiv = document.getElementById("imagelist");
data.results.forEach(photo => {
let listItem = document.createElement('img');
let source = photo.urls.regular;
console.log(source);
listItem.src = source;
listItem.height = '300';
listItem.width = '300';
listItem.onclick = (function() {SetBackground(this)});
myDiv.append(listItem);
});
});
}
//function setting background
function SetBackground(img){
var check;
check = confirm("Do you want to set this image as background?");
if(check == true){
var urlString = 'url(' + img.src + ')';
document.getElementById("mainBd").style.backgroundImage = urlString;
}
}