-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.js
64 lines (59 loc) · 1.65 KB
/
function.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
61
62
63
var page = 1;
function fetchCharacters(){
fetch(`https://rickandmortyapi.com/api/character/?page=${page}`)
.then(res => res.json())
.then(chars => {
var charsObj = chars.results;
var container=document.getElementById("characters");
container.innerHTML="";
for(var char of charsObj){
container.innerHTML+=`
<div>
<img src="${char.image}"/>
<div>
<span>${char.name}</span>
</div>
</div>
`
}
})
}
function fetchCharactersByName(){
var elem=document.getElementById("mytext");
fetch(`https://rickandmortyapi.com/api/character/?page=${page}&name=${elem.value.toLowerCase()}`)
.then(res => res.json())
.then(chars => {
var charsObj = chars.results;
var container=document.getElementById("characters");
container.innerHTML="";
for(var char of charsObj){
container.innerHTML+=`
<div>
<img src="${char.image}"/>
<div>
<span>${char.name}</span>
</div>
</div>
`
}
})
}
function fetchCharactersNext(){
var elem=document.getElementById("mytext");
page++;
if(elem.value === ''){
fetchCharacters();
}else{
fetchCharactersByName();
}
}
function fetchCharactersPrevious(){
var elem=document.getElementById("mytext");
page--;
if(elem.value === ''){
fetchCharacters();
}else{
fetchCharactersByName();
}
}
window.onload = fetchCharacters();