-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (34 loc) · 1.1 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
// Variables
const pokeApi = "https://pokeapi.co/api/v2/pokemon/";
const $form = $('form');
const $input = $('input[type = "text"]');
// Event listeners
$form.on("submit", getMon);
// Functions
function getMon(evt) {
evt.preventDefault();
let userInput = $input.val().toLowerCase();
$('form')[0].reset();
$('div.spriteContainer').replaceWith('<div class="spriteContainer"></div>')
$.ajax(pokeApi + userInput).then(function (data) {
render(data);
}, function (error) {
alert("Uh oh. Something went wrong.")
});
};
function render(pokeData) {
spriteURL = JSON.stringify(pokeData.sprites.front_default);
spriteShinyURL = JSON.stringify(pokeData.sprites.front_shiny)
console.log(spriteURL);
$('div.spriteContainer').replaceWith(`
<div class = "spriteContainer">
<div>
<img src = ${spriteURL}>
</div>
<h3>${pokeData.name.toUpperCase()}</h3>
<div>
<img src = ${spriteShinyURL}>
</div>
</div>
`);
};