Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
let url = 'http://api.thecatapi.com/v1/images/search'
// let randomButton = document.querySelector('.randomButton')
let randomCatImage = document.querySelector('.randomCatImage')
let input = document.querySelector('.input')
let searchButton = document.querySelector('.searchButton')
let form = document.querySelector('form')

// Part 1
// fetch(`${url}`)
// .then((res) => res.json())
// .then((data) => {
// console.log(data)
// })


// Part 2
// randomButton.addEventListener('click', handleClick)

// function handleClick(e) {
// e.preventDefault()

// fetch(`${url}`)
// .then(res => res.json())
// .then((data) => {
// randomCatImage.src=`${data[0].url}`
// })
// .catch((err) => console.log('This has failed', err))

// }


// Bonus
// console.log(input.value)



form.addEventListener('submit', clickHandler)

function clickHandler(e2) {

e2.preventDefault()

let categoryURL = `http://api.thecatapi.com/v1/images/search?api_key=live_M0df7MuutgzH6TbVTYJXtyAiYFTPIrIjuW45y8IA6g5E9gjX7jhVCXjLWIsbPuWd&category_ids=${input.value}`

fetch(`${categoryURL}`)
.then (res2 => res2.json())
.then ((data2) => {
randomCatImage.src=`${data2[0].url}`
})
.catch((err) => console.log('This too hath failed', err))
}