|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function main() { |
| 4 | + const allPokemonUrl = 'https://pokeapi.co/api/v2/pokemon?limit=151'; |
| 5 | + |
| 6 | + function fetchKantoPokemon() { |
| 7 | + fetch(allPokemonUrl) |
| 8 | + .then(response => response.json()) |
| 9 | + .then(function(allpokemon) { |
| 10 | + allpokemon.results.forEach(function(pokemon) { |
| 11 | + fetchPokemonData(pokemon); |
| 12 | + }); |
| 13 | + }); |
| 14 | + } |
| 15 | + fetchKantoPokemon(); |
| 16 | + |
| 17 | + function fetchPokemonData(pokemon) { |
| 18 | + const { url } = pokemon; |
| 19 | + fetch(url) |
| 20 | + .then(response => response.json()) |
| 21 | + .then(function(pokeData) { |
| 22 | + addPokemonToDOM(pokeData); |
| 23 | + }); |
| 24 | + } |
| 25 | + // function addPokemonToDOM(pokeData) { |
| 26 | + const container = document.createElement('div'); |
| 27 | + container.className = 'topContainer'; |
| 28 | + container.style.display = 'flex'; |
| 29 | + container.style.flexDirection = 'column'; |
| 30 | + container.style.width = '30%'; |
| 31 | + container.style.margin = '0 auto'; |
| 32 | + document.body.appendChild(container); |
| 33 | + const button = document.createElement('button'); |
| 34 | + button.id = 'btn'; |
| 35 | + button.innerText = 'Get Pokemon!'; |
| 36 | + container.appendChild(button); |
| 37 | + |
| 38 | + // what happens when u click the button |
| 39 | + |
| 40 | + const select = document.createElement('select'); |
| 41 | + select.id = 'select'; |
| 42 | + select.style.cursor = 'pointer'; |
| 43 | + select.style.display = 'none'; |
| 44 | + container.appendChild(select); |
| 45 | + button.addEventListener('click', function() { |
| 46 | + select.style.display = 'block'; |
| 47 | + }); |
| 48 | + const image = document.createElement('img'); |
| 49 | + image.style.display = 'flex'; |
| 50 | + image.style.width = '20%'; |
| 51 | + image.style.margin = '0 auto'; |
| 52 | + function addPokemonToDOM(pokeData) { |
| 53 | + const option = document.createElement('option'); |
| 54 | + option.innerText = pokeData.name; |
| 55 | + option.value = pokeData.sprites.front_default; |
| 56 | + option.style.cursor = 'pointer'; |
| 57 | + select.appendChild(option); |
| 58 | + } |
| 59 | + select.addEventListener('change', function() { |
| 60 | + console.log(select.value); |
| 61 | + image.src = select.value; |
| 62 | + document.body.appendChild(image); |
| 63 | + }); |
| 64 | +} |
| 65 | +window.addEventListener('load', main); |
0 commit comments