From 1fe8ee4a403ec8436868a29c26cb7ed83079210d Mon Sep 17 00:00:00 2001 From: jallen2787 Date: Mon, 2 Jan 2023 20:15:28 -0500 Subject: [PATCH] Random Cat API Lab --- lib/script.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/script.js b/lib/script.js index e69de29..8453eb3 100644 --- a/lib/script.js +++ b/lib/script.js @@ -0,0 +1,31 @@ +const url = "https://api.thecatapi.com/v1/images/search"; +const form = document.querySelector("form"); +const h1 = document.querySelector("h1"); +const container = document.querySelector(".container"); + +form.addEventListener("submit", handleSubmit); + +function handleSubmit(e) { + e.preventDefault(); + + const { id } = e.target.elements; + + fetch(`${url}/${id.value}`) + .then((res) => res.json()) + .then((data) => { + displayCharacters(data); + }) + .catch((err) => console.log("oops something went wrong", err)); +} + +function displayCharacters(character) { + const characterHTML = ` +
+

${character.name}

+

Hair color: ${character.hair_color}

+

Height: ${character.height}

+
+ `; + + container.insertAdjacentHTML("beforeend", characterHTML); +} \ No newline at end of file