Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change the color of a class based on data your recieving?

Ok so I am working on a Random Pokémon Generator and every time the user clicks the generate button it picks a random number between 1-905(Number of Pokémon in National Dex). I am able to show the image, name/id, and typing of the Pokémon that is generated. What im stuck on is how to change the color of the background of the container/class that the Pokémon is displayed in based on the typing of said Pokémon. Say the Pokémon’s typing is grass I would want the background to be a green color.
I really don’t know where to start to even do this.

I am using PokeApi

const pokemongenerated = document.getElementById('pokemongenerated');

const fetchPokemon = () => {
const array = [];
const randomID = Math.floor(Math.random() * 906) + 1;


    const url = `https://pokeapi.co/api/v2/pokemon/${randomID}`;
    array.push(fetch(url).then((res) => res.json()));

Promise.all(array).then((results) => {
     const pokemon = results.map((data) => ({
        name: data.name,
        image: data.sprites['front_default'],
        type: data.types.map((type) => type.type.name).join(', '),
        id: data.id,
        
       

    }));
    displayPokemon(pokemon);
  });
 };
const displayPokemon = (pokemon) => {
console.log(pokemon);
const pokemonHTMLString = pokemon
    .map(
        (pokemon) => `
    <ul class="card">
        <img class="card-image" src="${pokemon.image}"/>
        <h2 class="card-name"> ${pokemon.id}. ${pokemon.name}</h2>
        <p class="card-type">Type: ${pokemon.type}</p>

    </ul>
`
    )
    .join('');

pokemonGenerated.innerHTML = pokemonHTMLString;
};

fetchPokemon();

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

1.Create a typeCss on object
2.Make css styles for all type

const pokemongenerated = document.getElementById('pokemongenerated');

const fetchPokemon = () => {
const array = [];
const randomID = Math.floor(Math.random() * 906) + 1;


    const url = `https://pokeapi.co/api/v2/pokemon/${randomID}`;
    array.push(fetch(url).then((res) => res.json()));

Promise.all(array).then((results) => {
     const pokemon = results.map((data) => ({
        name: data.name,
        image: data.sprites['front_default'],
        type: data.types.map((type) => type.type.name).join(', '),
        typeCss: data.types.map((type)=>type.type.name).join('--'),
        id: data.id,
        
       

    }));
    displayPokemon(pokemon);
  });
 };
const displayPokemon = (pokemon) => {
console.log(pokemon);
const pokemonHTMLString = pokemon
    .map(
        (pokemon) => `
    <ul class="card pokemon-type type-${pokemon.typeCss}">
        <img class="card-image" src="${pokemon.image}"/>
        <h2 class="card-name"> ${pokemon.id}. ${pokemon.name}</h2>
        <p class="card-type">Type: ${pokemon.type}</p>

    </ul>
`
    )
    .join('');

document.querySelector('#pokemonGenerated').innerHTML = pokemonHTMLString;
};

fetchPokemon();
.pokemon-type[class*="water"]{
background:blue;
}
.pokemon-type[class*="grass"]{
background:green;
}

//so on
<div id="pokemonGenerated"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading