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 trialMahmoud Talass
9,274 PointsMy program isn't working properly
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<title>Image Generator</title>
</head>
<body>
<header>
<a href="#" class="logo">Random Image Generator</a>
<p>Click on the Generate Image Button to Generate a Random Image!</p>
</header>
<div class="main">
<div id="blank-sheet2"></div>
<div id='dimensions'>
<input type="text" placeholder="width" id="width" required>
<p>x</p>
<input type="text" placeholder="height" id="height" required>
</div>
<div class='button-div'>
<button type="button" id="btn2">Generate Image</button>
</div>
</div>
<div class="navigation">
<ul>
<li><a href="index.html">Our Color Generator Site</a></li>
<li><a href="">About</a></li>
<li><a href="">Donate</a></li>
</ul>
</div>
<script src="app.js"></script>
</body>
</html>
```
``` CSS
*, *::after, *::before {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Poppins', sans-serif;
}
header {
display: flex;
flex-direction: column;
justify-content: space-between;
align-content: center;
}
.logo {
font-size: 3rem;
text-decoration: none;
color: gray;
align-self: center;
margin-left: 20px;
}
header p {
font-size: 1rem;
align-self: center;
margin-left: 20px;
}
.main {
margin-top: 3rem;
display: flex;
flex-direction: column;
justify-content: center;
width: 100%;
}
#blank-sheet
/* #blank-sheet2 */ {
width: 275px;
height: 250px;
margin: 0 auto;
box-shadow: 0 0 5px 0;
}
#blank-sheet2 {
width: 100%;
height: 100%;
margin: 0 auto;
box-shadow: 0 0 5px 0;
}
#rgb-value {
font-size: 1rem;
/* width: 70%; */
margin-top: 20px;
padding-right: 0;
align-self: center;
}
#dimensions {
display: flex;
justify-content: center;
margin-top: 25px;
}
#width {
margin-right: 10px;
}
#height {
margin-left: 10px;
}
.button-div {
margin-top: 1.5rem;
align-self: center;
align-items: center;
}
#btn,
#btn2 {
font-size: 1.2rem;
padding: .4rem;
/* margin-right: 300px; */
}
.navigation {
text-align: center;
margin-top: 50px;
}
.navigation ul {
text-decoration: none;
display: flex;
flex-direction: column;
list-style: none;
justify-content: space-around;
align-items: center;
}
.navigation li {
background: rgb(239, 239, 239);
border-bottom: 1px solid gray;
padding: 12px;
margin-bottom: 20px;
}
.navigation a {
text-decoration: none;
color: gray;
}
@media (min-width: 800px) {
#blank-sheet,
#blank-sheet2 {
width: 550px;
height: 500px;
}
#rgb-value {
font-size: 1.5rem;
}
}
// =====================
// Image Page
// =====================
const imgButton = document.getElementById("btn2");
const containerSize = document.getElementById('blank-sheet2');
const width = document.getElementById('width');
const height = document.getElementById('height');
function randomImg() {
return containerSize.innerHTML = `<img src="https://picsum.photos/${width}/${height}" width=${width} height:${height} alt="image">`;
}
imgButton.addEventListener('click', randomImg);
Can someone please take a look at my code and tell me whats wrong. I am confused, I've been trying to get an image to appear everytime I click the 'generate image' button and nothing is happening. I also tried to add a width and height for the user to pick and nothing is working. Please help. Please help, thank you
1 Answer
Lucas Alle
Front End Web Development Techdegree Graduate 15,356 PointsIt seems that you're not inserting the image properly, when I inspect I see:
<img src="https://picsum.photos/[object HTMLInputElement]/[object HTMLInputElement]" width="[object" htmlinputelement]="" height:[object="" alt="image">
This is because in your javascript code: const width = document.getElementById('width'); const height = document.getElementById('height');
it is getting the elements: <input type="text" placeholder="width" id="width" required> <input type="text" placeholder="height" id="height" required> instead of the value of the inputs
So, To fix this I would change this code:
return containerSize.innerHTML = <img src="https://picsum.photos/${width.value}/${height.value}" width=${width.value} height:${height.value} alt="image">
;
the width.value and height.value will get the value from the input element, that's all you need to change
Hopefully, now it will work.
Mahmoud Talass
9,274 PointsMahmoud Talass
9,274 PointsI just tried your solution and the code is still not working for some reason. Thank you for taking the time to help though