0

I'm working on a website, and the pictures page I'm trying to create has 1 big picture, with smaller thumbnails underneath. I have a javascript function in the head:

function loadThumbnail(divID){ document.getElementById("mainPicture").src = document.getElementById(divID).src; document.getElementById("mainPicture").alt = document.getElementById(divId).alt; document.getElementById("caption").innerHTML = document.getElementById(divId).alt; }

The parameter divID is a misleading title, it's actually the ID of the img element that holds the thumbnail. The function is called whenever the user clicks a thumbnail. The .src bit works, but I can't get the function to change the caption to the picture. Any idea why?

Edit: Here's the HTML

<img id="mainPicture" src="images/food/chickenAndCheeseFries1.png" width="420" height="313" alt="Chicken Tenders and Cheese Fries">

<p id="caption">Chicken tenders and cheese fries</p>

<div onClick="loadThumbnail('t1')" id="thumbnail1" class="thumbnail">
                                <div id="thumbnailPic1" class="thumbnailPic">
                                    <img id="t1" src="images/food/buffaloAndHotWings1.png" width="105" height="80" alt="Sweet Chilly and Buffalo Wings">
                                </div>
                            </div>
3
  • Have you tried .getAttribute("alt") instead of .alt? Commented Mar 12, 2015 at 2:47
  • I just tried now, no luck Commented Mar 12, 2015 at 2:51
  • At the first line you're using divID and at the other you're using divId which is undefined thus resulting in an error. Commented Mar 12, 2015 at 3:01

1 Answer 1

2

Make sure the divID variable is used consistently, including the case. It looks like you typoed and used "divId", which won't work. Try it like this, "divID", in all cases.

function loadThumbnail(divID) {
  document.getElementById("mainPicture").src = document.getElementById(divID).src;
  document.getElementById("mainPicture").alt = document.getElementById(divID).alt;
  document.getElementById("caption").innerHTML = document.getElementById(divID).alt;
}

Case matters! divID != divId

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly it! Boy, do I feel a bit stupid. Can't believe that flew over of my head. Perhaps I should switch to a naming convention less prone to goofs like this. Thanks a ton!
It happens to everybody. Now you know to look for it. It may sound a little lazy, but careful use of copy/paste can avoid this problem if you have names that give you problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.