-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (50 loc) · 1.67 KB
/
script.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
51
52
53
54
55
56
57
58
59
60
function getDogList(){
$.get('https://dog.ceo/api/breeds/list/all' , function(data){
let dogBreed=data.message;
for(let breed in dogBreed){
$('.dog-breed').append(`<option val="${breed}">${breed}</option>`);
}
});
// after getting all dogs breed list we check whether the dog subreed exists or not
// if it exitst than we also make another dropdown list of subbreed
$('.dog-breed').change(function(){
let breed=$('.dog-breed :selected').text();
let url='https://dog.ceo/api/breed/' + breed + '/list';
$('.dog-subBreed').remove();
$.get(url , function(data){
if(data.message.length != 0){
$('.dog-breed').after(`<select class="dog-subBreed">`);
for(let content of data.message){
$('.dog-subBreed').append(`<option value=${content}>${content}</option>`);
}
}
});
});
}
// updating the list of the dogs breed
getDogList();
function fetchBreed(){
let dogBreed=$('.dog-breed :selected').text();
let dogSubBreed=$('dog-subBreed :selected').text();
let apiURL='https://dog.ceo/api/breed/' + dogBreed ;
// if subbreed exists then we add into the api url
if(dogSubBreed != ''){
apiURL+='/' + dogSubBreed;
}
apiURL+='/images';
$('.img-wrapper').remove();
$.ajax({
url:apiURL,
method:'GET',
success:function(data){
for(let i=0 ; i<data.message.length ; i++){
$(document.createElement('div')).attr('class' , 'img-wrapper').html(`<img src=${data.message[i]}>`).appendTo('.main');
}
}
});
}
function intialiseApp(){
$('.btn').click(fetchBreed);
}
// app start function
intialiseApp();