From b8374763c36d0428e5699326fac3f0f0206133c9 Mon Sep 17 00:00:00 2001 From: Jagerziel Date: Wed, 7 Dec 2022 20:32:46 -0500 Subject: [PATCH 1/2] Random Cat Image Generator Button Completed --- lib/script.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/script.js b/lib/script.js index e69de29..934e10a 100644 --- a/lib/script.js +++ b/lib/script.js @@ -0,0 +1,43 @@ +//Initialize variables for HTML +const url = "https://api.thecatapi.com/v1/images/search"; +const btnRand = document.querySelector('.randomButton'); +const reset = document.querySelector('.randomButton'); +const divCat = document.querySelector('.categoryCat') + +//fetch() will return after the other instances have completed +const yarnBall = (e) => { + // Prevents web page from reloading. + e.preventDefault(); + + fetch(url) + //pulls the inforamtion, parameter is a function + .then(res => { + //returns to the next .then statement + return res.json(); + }) + .then((data) => { + //runs displayCat function with data from api + displayCat(data) + console.log(data) + }) + //error handling + .catch(err => console.log('Something went wrong', err)); +} + +//Function for displaying cat pic +function displayCat(catPic) { + //declare HTML elements in a string within a variable + const catPicHTML = ` + Cat Pic + `; + //removes existing cat picture + while (divCat.childNodes.length > 0) { + divCat.removeChild(divCat.firstChild) + } + //inserts HTML block above with the HTML elements and information + divCat.insertAdjacentHTML("beforeend", catPicHTML); +} + + +//Get image upon click for random cat image +btnRand.addEventListener('click', yarnBall) From 8bccee6e542189d7f0aeb8074a9bf478ba09a1ab Mon Sep 17 00:00:00 2001 From: Jagerziel Date: Wed, 7 Dec 2022 20:34:53 -0500 Subject: [PATCH 2/2] Minor fix to old code --- lib/script.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/script.js b/lib/script.js index 934e10a..bfda590 100644 --- a/lib/script.js +++ b/lib/script.js @@ -1,7 +1,6 @@ //Initialize variables for HTML const url = "https://api.thecatapi.com/v1/images/search"; const btnRand = document.querySelector('.randomButton'); -const reset = document.querySelector('.randomButton'); const divCat = document.querySelector('.categoryCat') //fetch() will return after the other instances have completed