-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.04 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(dogBreed) {
fetch(`https://dog.ceo/api/breed/${dogBreed}/images/random`)
.then(response => response.json())
.then(responseJson =>
displayResults(responseJson))
.catch(error => alert('Something went wrong. Try again later.'));
}
function displayResults(responseJson) {
console.log(responseJson);
//replace the existing image with the new one
let dogBreed = $('#breed').val()
let html = `<img src="${responseJson.message}" />`
if (responseJson.code === 404) {
$('.results h2').html(`Sorry, no dog found. Please try again.`)
$('.results-img').html(``)
}else {
$('.results h2').html(`Look at the ${dogBreed}!`)
$('.results-img').html(html)
}
//display the results section
$('.results').removeClass('hidden');
}
function watchForm() {
$('form').submit(event => {
event.preventDefault();
let dogBreed = $('#breed').val().toLowerCase()
getDogImage(dogBreed);
});
}
$(function() {
console.log('App loaded! Waiting for submit!');
watchForm();
});