From 542474e8ccdb29ced654dd928b850502f5a8916c Mon Sep 17 00:00:00 2001 From: Kan5661 Date: Wed, 7 Dec 2022 13:36:46 -0500 Subject: [PATCH 1/2] button generate random cat image --- lib/script.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/script.js b/lib/script.js index e69de29..2e43a97 100644 --- a/lib/script.js +++ b/lib/script.js @@ -0,0 +1,21 @@ +url = 'https://api.thecatapi.com/v1/images/search' + +randomButton = document.querySelector('.randomButton') +imageDisplay = document.querySelector('img') + +randomButton.addEventListener('click', getImage) + +function getImage() { + fetch(url) + .then(res => { + return res.json() + }) + .then(data => { + imageDisplay.src = data[0].url + console.log(data[0]) + }) + .catch(error => { + console.error(error) + }) + + } \ No newline at end of file From da7e20b16180b0b4d254129e71b7b5a961a0839a Mon Sep 17 00:00:00 2001 From: Kan5661 Date: Wed, 7 Dec 2022 20:34:22 -0500 Subject: [PATCH 2/2] bonus --- lib/script.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/script.js b/lib/script.js index 2e43a97..0fc1285 100644 --- a/lib/script.js +++ b/lib/script.js @@ -2,9 +2,15 @@ url = 'https://api.thecatapi.com/v1/images/search' randomButton = document.querySelector('.randomButton') imageDisplay = document.querySelector('img') - randomButton.addEventListener('click', getImage) + +categoryUrl = 'https://api.thecatapi.com/v1/images/search?category_ids=' + +inputBox = document.querySelector('.input') +searchButton = document.querySelector('.searchButton') +searchButton.addEventListener('click', getImgByCategory) + function getImage() { fetch(url) .then(res => { @@ -17,5 +23,15 @@ function getImage() { .catch(error => { console.error(error) }) +} - } \ No newline at end of file +function getImgByCategory(input) { + input.preventDefault() + fetch(categoryUrl + inputBox.value.toString()) + .then(response => { + return response.json() + }) + .then(data => { + imageDisplay.src = data[0].url + }) +} \ No newline at end of file