-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (34 loc) · 1.07 KB
/
index.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
// console.log('hello');
const image = document.querySelector('#dog-img');
const select = document.querySelector('.breeds')
function showDogPic() {
fetch('https://dog.ceo/api/breeds/list/all')
.then(resopnes => {
return resopnes.json();
})
.then(data => {
const breedObj= data.message;
const breedArray = Object.keys(breedObj);
for(let i=0;i<breedArray.length;i++) {
const option = document.createElement('option')
option.value = breedArray[i];
option.innerText = breedArray[i];
select.appendChild(option);
}
console.log(breedArray);
})
select.addEventListener('change',event => {
console.log(event.target.value);
fetch(`https://dog.ceo/api/breed/${event.target.value}/images`)
.then(response => {
return response.json();
})
.then(data => {
// console.log(data.message);
let source = data.message;
let index = Math.floor(Math.random()*source.length);
image.src = source[index];
})
});
}
showDogPic();