Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Changing 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
Erik Nuber
20,629 Points

You 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
}
Erik Nuber
Erik Nuber
20,629 Points

You 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
Steven Parker
221,938 Points

:hand: Just 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.

Steven Parker
Steven Parker
221,938 Points

:point_right: There 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

Should I add the other images that I want to replace the original once the 'button' is clicked into the html under the original?