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 trialRashaun Ewing
15,711 PointsChanging Image on Button click
I'm attempting to change and image when the 'button' is clicked.
<div class="flex-item2">
<h1 class="name">Keystone</h1>
<div>
<img id='stone' src="..//../Images/IMG_2212.jpg">
<p>This section is going to be dedicated to my furry little friend.laslflasjflasjlfjasl;jfsa
aljajfjsalfjlsjlfjsljflsjlfjsljfljslfslajlsjfjlaksfljsfjlsajflsjalfjsaj;fjaslflksajlfjlasjflj
alsflsajlfjlasjfljslfjsjdjflskdjflajsljflsajfjlsjfjsjfsjdljflasjfjlsjdkfjlsjfljlsflsjfajslfdk
aljdfjalsjfljslfjsdkklsjlfjlsdjfjldjjsldjflskdjflkdsnfksdnfnsdkdnlanlnfnkkdladskfnndlasdda.</p>
</div>
<div>
<button type="button" onclick="changeImage('stone')">Change Me</button>
</div>
function changeImage('stone'){
image = id = 'stone'.getElementById();
document.getElementbyId('sleep2'). src="..//../Images/IMG_3828.jpg";
}
2 Answers
Erik Nuber
20,629 PointsYou don't need to pass the function a variable.
function changeImage(){
document.getElementById('stone').src="..//../Images/IMG_3828.jpg"; //if this is the new image it goes after the = sign
}
Steven Parker
231,141 PointsThere seems to be a number of issues here:
- in the function declaration, the parameter should not be in quotes
- you have a line that doesn't make sense that appears to be setting global variables image and id
- JavaScript is case-sensitive — getElementById must be spelled with a capital "B"
- you seem to be changing the src of a element with id "sleep2", but there's no element with that id in the HTML
- since you pass the id to the function, you might want to use it instead of a literal name
Rashaun Ewing
15,711 PointsShould I add the other images that I want to replace the original once the 'button' is clicked into the html under the original?
Erik Nuber
20,629 PointsErik Nuber
20,629 PointsYou could also change your ID tag for the image similarly by adding in
document.getElementByID('stone').id = "newIDNameHere"
Yes, you don't have to change the ID, it isn't necessary and could be more hassle to implement your code. However, if you are changing a picture (especially from something unrelated), you might also want to change your ID to reflect that change. However, the above code works, the method is available for a reason.
Steven Parker
231,141 PointsSteven Parker
231,141 PointsJust because you can change an id doesn't mean that you should.
In general, I'd think this is a bad practice. Anytime you may be tempted to do this, there's probably a better way to accomplish whatever you're trying to do.