-
Notifications
You must be signed in to change notification settings - Fork 0
/
doggos.js
50 lines (42 loc) · 1.56 KB
/
doggos.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
const BREEDS_URL = "https://dog.ceo/api/breeds/list/all";
const select = document.querySelector(".breeds");
let imgArea = document.querySelector(".doggos");
let loader = document.querySelector(".loader");
fetch(BREEDS_URL)
.then(function(response) {
return response.json();
})
.then(function(data) {
const breedsObject = data.message;
const breedsArray = Object.keys(breedsObject);
for (let i = 0; i < breedsArray.length; i++) {
const option = document.createElement("option");
option.value = breedsArray[i];
option.innerText = breedsArray[i].charAt(0).toUpperCase() + breedsArray[i].slice(1);
option.style.textTransform = "capitalize";
select.appendChild(option);
}
})
function imageLoader(url) {
loader.style.display = "block";
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
const img = document.createElement("img");
img.src = data.message;
document.querySelector(".doggos").appendChild(img);
img.addEventListener("load", function() {
loader.style.display = "none";
img.style.boxShadow = "0 5px 20px rgba(0, 0, 0, 0.75)";
})
})
}
select.addEventListener("change", function(event) {
let dogUrl = `https://dog.ceo/api/breed/${event.target.value}/images/random`;
if (imgArea.lastChild) {
imgArea.removeChild(imgArea.lastChild);
}
imageLoader(dogUrl);
})