Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Rishi B](https://secure.gravatar.com/avatar/20d898d4820d608733eee19652f40d8e?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Rishi B
13,866 Pointswhy 'breed' variable is blank ?
I couldn't get the picture of dog that matches to selected option.
const select = document.getElementById('breeds');
const card = document.querySelector('.card');
const form = document.querySelector('form');
// ------------------------------------------
// FETCH FUNCTIONS
// ------------------------------------------
function fetchData(url){
return fetch(url).then(res => res.json())
}
fetchData('https://dog.ceo/api/breeds/list')
.then( data => generateOptions(data.message))
const breed = select.value;
fetchData(`https://dog.ceo/api/breed/${breed}/images/random`)
.then( data => generateImage(data.message) )
// ------------------------------------------
// HELPER FUNCTIONS
// ------------------------------------------
function generateOptions(data){
const options = data.map( item => `
<option value=${item}>${item}</option>
`).join('');
select.innerHTML = options;
}
function generateImage(data){
card.innerHTML = `
<img src='${data}' >
<p>click to view images of ${select.value}s</p>
`;
}
2 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsAs the page first loads, no selection has been made yet so the value is empty. That's why the code in the video only loads a random image to start with.
Later in the lesson, event listeners will be added that will load the breed image after a selection is made by the user.
![Rishi B](https://secure.gravatar.com/avatar/20d898d4820d608733eee19652f40d8e?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Rishi B
13,866 PointsThank you so much !!!
Rishi B
13,866 PointsRishi B
13,866 Pointscouldn't set the image to match the first option in the Options of 'select' element??
above line is not working...
Steven Parker
230,688 PointsSteven Parker
230,688 PointsNo, it's also a timing issue. The "fetch" operation is asynchronous, so the assignment happens before any options have been loaded.
To guarantee the sequence, you'd need to make the image fetch part of another "then" chained to the options fetch: