-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoke.js
More file actions
30 lines (28 loc) · 999 Bytes
/
poke.js
File metadata and controls
30 lines (28 loc) · 999 Bytes
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
var input = document.getElementById("name");
input.addEventListener("keypress", function(event) {
// If the user presses the "Enter" key on the keyboard
if (event.key === "Enter") {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
document.getElementById("fetchbtn").click();
}
});
async function fetchdata(){
try{
const pokename=document.getElementById("name").value.toLowerCase();
const response=await fetch(`https://pokeapi.co/api/v2/pokemon/${pokename}`);
if(!response.ok){
throw new Error("Pokemon not found!");
}
const data=await response.json();
const pokesprite =data.sprites.front_default;
const imgelement=document.getElementById("sprite");
imgelement.src=pokesprite;
imgelement.style.display="block";
console.log(data);
}
catch(error){
console.error(error);
}
}