-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiSearchBar.js
More file actions
81 lines (64 loc) · 1.86 KB
/
apiSearchBar.js
File metadata and controls
81 lines (64 loc) · 1.86 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const searchApi = document.getElementById("searchApi");
const listApi = document.getElementById("listApi");
let pokemonCharacters = [];
//console.log(pokemonCharacters.result;
searchApi.addEventListener('keyup', (e) => {
const searchString = e.target.value;
if (searchString && searchString.trim().length > 0){
const filterCharacter = pokemonCharacters.filter( (character) => {
return (character.name.toLowerCase().includes(searchString)
);
});
setList(filterCharacter);
}else{
clearList();
}
});
const loadCharacter = async () => {
try {
const result = await fetch('https://pokeapi.co/api/v2/pokemon/?limit=70');
let temp = await result.json();
pokemonCharacters = temp.results;
//displayCharacters(pokemonCharacters);
//console.log(pokemonCharacters);
} catch (err){
console.error(err);
};
};
/*
const displayCharacters = (characters) => {
const htmlString = characters
.map((character) => {
return `
<li class="character">
<h2>${character.name}<h2>
</li>
`;
}).join('');
listApi.innerHTML = htmlString;
};
*/
function setList(group){
clearList();
for (const keyword of group){
const item = document.createElement("li");
const text = document.createTextNode(keyword.name);
item.appendChild(text);
listApi.appendChild(item);
}
if(group.length === 0){
setNoResult();
}
}
function clearList(){
while(listApi.firstChild){
listApi.removeChild(listApi.firstChild);
}
}
function setNoResult(){
const item = document.createElement("li");
const text = document.createTextNode('No result found');
item.appendChild(text);
listApi.appendChild(item);
}
loadCharacter();