Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Dog Photos and List to DOM #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Intro to AJAX Practice Tasks</title>
<script src="src/index.js" charset="utf-8"></script>
<script src="src/index.js" charset="utf-8" defer></script>
</head>
<body>
<h1>Dog CEO</h1>
Expand Down
57 changes: 57 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
console.log('%c HI', 'color: firebrick')
let dogsList
const dogBreedList = document.getElementById('dog-breeds')
const dogImages = document.getElementById('dog-image-container')
const dropDownOptions = document.querySelectorAll('option')
const breedDropDown = document.getElementById('breed-dropdown')

const addBreedClickListener = (breed) => {
document.getElementById(`${breed}`).addEventListener('click', () => {
document.getElementById(`${breed}`).style.color = 'pink'
})
}

const grabImages = () => {
fetch("https://dog.ceo/api/breeds/image/random/4")
.then(resp => resp.json())
.then(data => {
data.message.forEach(dog => {
const createImage = document.createElement('img')
dogImages.appendChild(createImage).src = `${dog}`
})
})
}

const grabBreeds = () => {
fetch("https://dog.ceo/api/breeds/list/all")
.then(resp => resp.json())
.then(data => {
dogsList = data.message
return data
})
.then(data => {
Object.keys(data.message).forEach(breed => {
const createListItem = document.createElement('li')
createListItem.id = `${breed}`
dogBreedList.appendChild(createListItem).textContent = breed
addBreedClickListener(breed)
})
})
}


grabImages()
grabBreeds()


breedDropDown.onchange = () => {
const filteredDogsList = Object.keys(dogsList).filter(dog => dog.startsWith(breedDropDown.value))
dogBreedList.innerHTML = ''
filteredDogsList.forEach(breed => {
const createListItem = document.createElement('li')
createListItem.id = `${breed}`
dogBreedList.appendChild(createListItem).textContent = breed
addBreedClickListener(breed)})
}



Comment on lines +41 to +58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grabImages()
grabBreeds()
breedDropDown.onchange = () => {
const filteredDogsList = Object.keys(dogsList).filter(dog => dog.startsWith(breedDropDown.value))
dogBreedList.innerHTML = ''
filteredDogsList.forEach(breed => {
const createListItem = document.createElement('li')
createListItem.id = `${breed}`
dogBreedList.appendChild(createListItem).textContent = breed
addBreedClickListener(breed)})
}
grabImages()
grabBreeds()
breedDropDown.onchange = () => {
const filteredDogsList = Object.keys(dogsList).filter(dog => dog.startsWith(breedDropDown.value))
dogBreedList.innerHTML = ''
filteredDogsList.forEach(breed => {
const createListItem = document.createElement('li')
createListItem.id = `${breed}`
dogBreedList.appendChild(createListItem).textContent = breed
addBreedClickListener(breed)})
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You kill me with all the extra spaces!