-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokedex.js
48 lines (40 loc) · 1.46 KB
/
pokedex.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
$(document).ready(function() {
$('#searchBtn').on('click', function() {
var searchData = $('#searchInput').val().toLowerCase();
getData(searchData);
});
function getData(searchData) {
$.ajax({
url: `https://pokeapi.co/api/v2/pokemon/${searchData}/`,
type: 'GET',
dataType: 'json',
success: function(data) {
displayData(data);
},
error: function(jqXHR, textStatus, errorThrown) {
$('#pokemonInfo').html('Pokemon not found!');
}
});
}
function displayData(data) {
var pokemonImage = data.sprites.front_default;
var pokemonName = data.name;
var pokemonHeight = data.height;
var pokemonWeight = data.weight;
var pokemonTypes = data.types.map(type => type.type.name).join(', ');
var pokemonNumber = data.id;
var html = `
<img src="${pokemonImage}" alt="${pokemonName}">
<h2>${pokemonName}</h2>
<p>Height: ${pokemonHeight} | Weight: ${pokemonWeight}</p>
<p>Type: ${pokemonTypes}</p>
<p>Pokédex Number: ${pokemonNumber}</p>
`;
$('#pokemonInfo').html(html);
}
});
document.getElementById("searchInput").addEventListener("keyup", function(event) {
if (event.key === "Enter") {
document.getElementById("searchBtn").click();
}
});