Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body class="wrapper l-stack-xxl">
	<h1>Pokédex</h1>

	<div class="forms-container">
		<form method="post" id="form-number">
			<h2>Search</h2>
			<p>Search for a Pokémon by its number.</p>
			<div class="l-stack-xxs">
				<label for="input-number">Pokémon number (1 – 898)</label>
				<input id="input-number" type="number" inputmode="numeric" pattern="[0-9]*" name="input-number" min="1" max="898" required>
			</div>
			<button id="submit-number" type="submit" name="submit-number">Show Pokémon</button>
		</form>

		<form method="post" id="form-random">
			<h2>Random</h2>
			<p>Show a random Pokémon.</p>
			<button id="submit-random" type="submit" name="submit-random">Show a random Pokémon</button>
		</form>
	</div>

	<!--<form id="form-name">
		<label for="input-name">Pokémon name</label>
		<div class="l-input-button">
			<input id="input-name" type="text" name="input-name" required>
			<button id="submit-name" type="submit" name="submit-name">Show Pokémon</button>
		</div>
	</form>-->

	<ul role="list" id="list" class="list-pokemon"></ul>
</body>
              
            
!

CSS

              
                /* This pen is using external base CSS stylesheet(s) in the CSS Settings */

:root {
	--page-max-inline-size: 100rem;
}

* {
	margin: 0;
}

body {
	padding-block: 1rem;
	text-align: center;
}

form {
	display: flex;
	flex-direction: column;
	gap: 1rem;
}

form > :last-child {
	margin-block-start: auto;
}

figure {
	border: var(--bw-1) solid var(--c-bg-3);
	border-radius: var(--border-radius);
}

figure img {
	max-inline-size: 10rem;
	border-start-start-radius: var(--border-radius);
	border-start-end-radius: var(--border-radius);
}

figure figcaption {
	padding: var(--padding-xs);
	background-color: var(--c-bg-2);
	border-end-start-radius: var(--border-radius);
	border-end-end-radius: var(--border-radius);
}

.forms-container {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	gap: var(--s-800);
}

.list-pokemon {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(min(100%, 10rem), 1fr));
	justify-items: center;
	gap: var(--s-400);
}

.list-pokemon:empty {
	display: none;
}

.pokemon-type {
	padding-block: var(--padding-xxxs);
	padding-inline: var(--padding-sm);
	background-color: var(--c-bg-a);
	border-radius: var(--border-radius);
}

              
            
!

JS

              
                // RESTful API "PokéAPI" (pokeapi.co)
// Endpoint: "https://pokeapi.co/api/v2/pokemon/{id or name}/"
const baseApiUrl = "https://pokeapi.co/api/v2/pokemon/";
const maxPokeNumber = 898;

// Elements in the DOM
const formNumber = document.querySelector("#form-number");
const formRandom = document.querySelector("#form-random");
const list = document.querySelector("#list");

formNumber.addEventListener("submit", (e) => {
	e.preventDefault();

	const inputNumber = e.currentTarget.elements["input-number"];

	// Sanitize and store the number that the user has entered
	const pokeNumber = parseInt(inputNumber.value);

	// Clear the input value
	inputNumber.value = "";

	// Remove current Pokémon
	list.querySelectorAll(":scope > *").forEach((pokemon) => pokemon.remove());

	// Fetch the Pokémon data using the API
	getPokemonData(pokeNumber);
});

formRandom.addEventListener("submit", (e) => {
	e.preventDefault();

	// Remove current Pokémon
	list.querySelectorAll(":scope > *").forEach((pokemon) => pokemon.remove());

	// Fetch the Pokémon data using the API
	getPokemonData(getRandomIntInclusive(1, maxPokeNumber));
});

function getPokemonData(idNumber) {
	const requestURL = baseApiUrl + idNumber;

	fetch(requestURL)
		.then((response) => response.json())
		.then((pokemonData) => {
			const pokemonElement = createPokemon(pokemonData);
			return addPokemonToList(pokemonElement, list);
		})
		.catch((error) => console.log(error));
}

function createPokemon({ id, name, sprites, types }) {
	// Figure as a container
	const pokemon = document.createElement("figure");

	// Image
	const image = document.createElement("img");
	image.src = `${sprites["other"]["official-artwork"]["front_default"]}`;
	image.alt = `Pokémon number ${id}, called ${name}`;
	image.width = "475";
	image.height = "475";
	pokemon.append(image);

	// Figcaption for the info
	const info = document.createElement("figcaption");
	info.classList.add("l-stack-xxs");

	const infoNumber = document.createElement("p");
	infoNumber.innerText = id;
	info.append(infoNumber);

	const infoName = document.createElement("p");
	infoName.innerText = name[0].toUpperCase() + name.slice(1);
	infoName.classList.add("text-500", "weight-bold");
	info.append(infoName);

	const infoTypes = document.createElement("ul");
	infoTypes.setAttribute("role", "list");
	infoTypes.classList.add("l-cluster-xs", "text-300");
	infoTypes.setAttribute("data-layout", "justify-center");
	types.forEach((type) => {
		const typeElement = document.createElement("li");
		typeElement.innerText = type["type"]["name"];
		typeElement.classList.add("pokemon-type");
		infoTypes.append(typeElement);
	});
	info.append(infoTypes);

	pokemon.append(info);

	return pokemon;
}

function addPokemonToList(pokemon, list) {
	const li = document.createElement("li");
	li.append(pokemon);
	list.append(li);
}

function getRandomIntInclusive(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	const randomNumber = Math.floor(Math.random() * (max - min + 1) + min);
	return randomNumber;
}

              
            
!
999px

Console