-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (36 loc) · 1.11 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
37
38
39
40
'use strict';
function getDogImage(optionDogBreed) {
// console.log('getDogImage()');
fetch(`https://dog.ceo/api/breed/${optionDogBreed}/images/random`)
.then(response => {
if(response.ok) return response.json()
throw Error()
})
.then(responseJson =>
displayResults(responseJson))
.catch(error =>
alert('Something went wrong. Try again later.'));
}
function displayResults(responseJson) {
// console.log(responseJson);
// console.log('displayResults()');
$('.results').html('');
$('.results').append(
`<img src="${responseJson.message}" class="results-img" alt="random dog image">`
)
//display the results section
$('.results').removeClass('hidden');
}
function watchForm() {
$('form').on('submit', function (event) {
// console.log('watchForm()');
event.preventDefault();
let optionDogBreed = $("#dog-breed").val();
// console.log(optionDogBreed);
getDogImage(optionDogBreed);
});
}
$(function () {
console.log('App loaded! Waiting for submit!');
watchForm();
});