im kinda new to javascript. using this function (which was my answer to a previous question in the assignment) i am supposed to load a list of 50 links with the class “collection-item” within an element with the id “listing”. how do i go about this. the teacher doesnt explain very well.
the previous question was to make an HTTP request for a collection of pokemon data using https://pokeapi.co/api/v2/pokemon/?limit=50&offset=50.
async function getPokemon(url){
try{
let response = await fetch(url);
let result = await response.json();
getListing(result);
}catch(e){
console.log(e);
}
}
getPokemon("https://pokeapi.co/api/v2/pokemon/?limit=50&offset=50")
Instead of ‘list +=’ try ‘list.innerHTML =’
You are trying to use a link to get the data that fetch has already pulled for you. The a element and the onclick property are not necessary innerrHTML = is good enough to display your data in your div.
I didnt look at how you are accessing the list I suppose you are ok with that
ok now im really confused. im trying to list all 50 pokemon from the api onto a page. i am not getting the array and subsequently the list to print. so im supposed to have pokemon names being printed in a list, instead i get no output. “I didnt look at how you are accessing the list I suppose you are ok with that”. is this supposed to mean that getAllPokemon doesnt access the list to print out the names? i thought this function
async function getAllPokemon(){
let url = "https://pokeapi.co/api/v2/pokemon/?limit=50&offset=50";
try{
let response = await fetch(url);
let result = await response.json();
displayAllPokemon(result);
}catch(e){
console.log(e);
}
}
would call the array which i can use to print a list of names using this function
function displayAllPokemon(pokemon){
let list=document.getElementById("listing").innerHTML;
let i=0;
console.log(pokemon);
for(i=0; i<50; i++){
list.innerHTML+=`<li><a class="collection-item" href="#" id="${pokemon.name[i]}" onclick=getPokemonDetail('${pokemon.name[i]}')>${pokemon.name[i]}</a></li>`;
}
console.log(list);
}
results is an array inside of an object so use dot notation to access results thus: “pokemon.results” and then you can loop through results because you now have access to it.