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
Davis Rousseau
6,670 PointsHaving trouble fixing this.
I'm working on a personal project. When I click on the thumbnail how can I get the download image button to change based on that image. Here's my codepen http://codepen.io/davisrousseaudesign/pen/fCEcm
3 Answers
Shawn Flanigan
Courses Plus Student 15,815 PointsI think I see what you're saying. To solve this, I would give the download link an id...something like
<a id="downloadLink" href="http://www.medline.com/media/mkt/products/furnishings/textiles/cubicle-curtains/allegro/allegro-cabernet.jpg">
Then I might add a third parameter to your showImage function, called something like downloadHref and pass it through your onClick events in your thumbnails.
So, your thumb onClick might look like this:
onclick="showImage('http://www.medline.com/media/mkt/products/furnishings/textiles/cubicle-curtains/allegro/allegro-cabernet.jpg', 'Carbanet text for the right side.', 'http://www.medline.com/media/mkt/products/furnishings/textiles/cubicle-curtains/allegro/allegro-cabernet.jpg');"
Then you can change the href attribute by using some javascript within your showImage function, like so:
function showImage(imgName, imgText, downloadHref) {
var curImage = document.getElementById('currentImg');
var textDiv = document.getElementById('rightText');
curImage.src = imgName;
curImage.alt = imgName;
curImage.title = imgName;
textDiv.innerHTML = imgText;
var downloadLink = document.getElementById('downloadLink');
downloadLink.href=downloadHref;
}
Does that make sense?
Jason Gong
4,537 Pointshttp://codepen.io/Tootley/pen/qEWQxP
Here's the solution. I just added a id to the download button (dlLink) and used your onclick function to change the href to the imgName parameter you send into the function everytime you click the pictures!
See if that works for you.
Shawn Flanigan
Courses Plus Student 15,815 PointsI was going to use the imgName parameter, too, but noticed that he was trying to link to a larger file (with a different name), so this might not work properly. If imgName was a single word, like "cabarnet" or "cookie", and we knew that the file structure and naming conventions would always be the same, we could concatenate imgName to pieces of the image urls, which would make for cleaner html and javascript...and easier maintenance.
Jason Gong
4,537 PointsOh that's true, I missed the -large at the end of the jpg. I guess you could insert that part of the url in there if we stayed with the current naming conventions. I think the solution you posted works best with what he has right now!
Davis Rousseau
6,670 PointsI'm new to javascript. At first I thought I solved but notice it only works when the page first load. Shawn this was exactly what I was looking for. Thanks everyone!
Shawn Flanigan
Courses Plus Student 15,815 PointsGlad I could help!
Shawn Flanigan
Courses Plus Student 15,815 PointsShawn Flanigan
Courses Plus Student 15,815 PointsMaybe I don't understand your question, but it looks like it's working the way you want it to. Did you already solve this?