This repository was archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (55 loc) · 1.84 KB
/
script.js
File metadata and controls
68 lines (55 loc) · 1.84 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
function main() {
const buttonElement = document.createElement("button");
document.body.appendChild(buttonElement);
buttonElement.id = "button";
buttonElement.innerHTML = "GetPokemon!";
const formElement = document.createElement("form");
document.body.appendChild(formElement);
const selectElement = document.createElement("select");
formElement.appendChild(selectElement);
selectElement.id = "select";
const divElement = document.createElement("div");
document.body.appendChild(divElement);
divElement.id = "div";
const select = document.getElementById("select");
const url = "https://pokeapi.co/api/v2/pokemon/?limit=151"
const button = document.getElementById("button");
const div = document.getElementById("div")
img = document.createElement("img")
button.addEventListener('click',
function fetchData() {
fetch(url)
.then(async (res) => {
if (!res.ok) {
throw `ERROR: ${res.status} ${res.statusText}`
}
const jsonData = await res.json()
pokemonArr = jsonData.results
pokemonArr.map((element) => {
let option = document.createElement("option")
select.appendChild(option)
option.value = element.name
option.innerHTML = element.name
})
})
.catch((error) => {
console.log(error);
})
})
select.onchange = function addPokemonToDOM() {
fetch(`https://pokeapi.co/api/v2/pokemon/${select.value}`)
.then(async (res1) => {
if (!res1.ok) {
throw `ERROR: ${res1.status} ${res1.statusText}`
}
const imageData = await res1.json()
const image = imageData.sprites.front_default
div.appendChild(img)
img.src = image
})
.catch((error) => {
console.log(error);
}
)
}
} main()