File tree 3 files changed +62
-0
lines changed
hyperskill/07_dog_glossary/02
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ window . onload = function ( ) {
2
+ const randomDogButton = document . getElementById ( 'button-random-dog' ) ;
3
+ const contentDiv = document . getElementById ( 'content' ) ;
4
+ const inputBreed = document . getElementById ( 'input-breed' ) ;
5
+ const showBreedButton = document . getElementById ( 'button-show-breed' ) ;
6
+
7
+ const dogApiBaseUrl = 'https://dog.ceo/api' ;
8
+
9
+ randomDogButton . addEventListener ( 'click' , async function ( ) {
10
+ const response = await fetch ( `${ dogApiBaseUrl } /breeds/image/random` ) ;
11
+
12
+ if ( ! response . ok ) {
13
+ contentDiv . innerHTML = 'Error fetching random dog' ;
14
+ return ;
15
+ }
16
+
17
+ const data = await response . json ( ) ;
18
+
19
+ if ( data . status === 'success' ) {
20
+ contentDiv . innerHTML = `<img src="${ data . message } " alt="Random dog" />` ;
21
+ } else {
22
+ contentDiv . innerHTML = 'Error fetching random dog' ;
23
+ }
24
+ } ) ;
25
+
26
+ showBreedButton . addEventListener ( 'click' , async function ( ) {
27
+ const breed = inputBreed . value . toLowerCase ( ) ;
28
+ const response = await fetch ( `${ dogApiBaseUrl } /breed/${ breed } /images/random` ) ;
29
+
30
+ if ( ! response . ok ) {
31
+ contentDiv . innerHTML = '<p>Breed not found!</p>' ;
32
+ return ;
33
+ }
34
+
35
+ const data = await response . json ( ) ;
36
+
37
+ if ( data . status === 'success' ) {
38
+ contentDiv . innerHTML = `<img src="${ data . message } " alt="${ breed } " />` ;
39
+ } else {
40
+ contentDiv . innerHTML = '<p>Breed not found!</p>' ;
41
+ }
42
+ } ) ;
43
+ }
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Dog Glossary</ title >
6
+ < link rel ="stylesheet " href ="style.css ">
7
+ < script src ="app.js " defer > </ script >
8
+ </ head >
9
+ < body >
10
+ < h1 > Dog Glossary</ h1 >
11
+ < button id ="button-random-dog "> Show Random Dog</ button >
12
+ < input type ="text " id ="input-breed " placeholder ="Enter a breed ">
13
+ < button id ="button-show-breed "> Show Breed</ button >
14
+ < div id ="content "> </ div >
15
+ </ body >
16
+ </ html >
Original file line number Diff line number Diff line change
1
+ * {
2
+ box-sizing : border-box;
3
+ }
You can’t perform that action at this time.
0 commit comments