Pete Daugenti
Pete Daugenti

Reputation: 1

Image Swap -- Uncaught TypeError: Cannot set property 'src' of null

I'm working through the textbook for a class as I usually do for practice. I'm currently working on an image swapping page where clicking on a thumbnail "swaps" out the main image. The images when clicked become enlarged as if it were a link to another web page. My code is exactly as it is in the book but I get an error. The code is:

var $ = function(id){
return document.getElementById(id);
};

window.onload = function(){
var listNode = $("image_list");
var captionNode = $("caption");
var imageNode = $("main_image");

var imageLinks = listNode.getElementsByTagName("a");

// process image links
var i, image, linkNode, link;
for (i = 0; i < imageLinks.length; i++){
    linkNode = imageLinks[i];

    // preload image
    image = new Image();
    image.src = linkNode.getAttribute("href");

    // attach event handler
    linkNode.onclick = function(evt){
        link = this;  // "this" is the link that was clicked

        // set new image and caption
        // the image selected is the "href" and the caption is the title of each image link
        imageNode.src = link.getAttribute("href");
        captionNode.firstChild.nodeValue = link.getAttribute("title");

        // cancel the default action of the event
        if (!evt){
            evt = window.event;
        }
        if (evt.preventDefault){
            evt.preventDefault();
        }else{
            evt.returnFalse = false;
        }
    };
}
// set focus on first image link
imageLinks[0].focus();

};`

The error is thrown at imageNode.src = link.getAttribute("href"); My HTML is:

<body>
        <h1>Image Swap With JavaScript</h1>
        <p>Click on an image to enlarge.</p>
        <ul id="image_list">
            <li><a href="images/photo1.jpg" title="Golden Gate">
                <img src="thumbnails/thumb1.jpg" alt=""></a></li>
            <li><a href="images/photo2.jpg" title="Rocky Coast">
                <img src="thumbnails/thumb2.jpg" alt=""></a></li>
            <li><a href="images/photo3.jpg" title="Ocean Side">
                <img src="thumbnails/thumb3.jpg" alt=""></a></li>
        </ul>
        <h2 id="caption">Ocean Side</h2>
        <p><img id="main-image" src="images/photo3.jpg" alt=""></p>
    </body>

Why am I getting this error?

Upvotes: 0

Views: 67

Answers (1)

pavan kumar
pavan kumar

Reputation: 823

Answer first,then lecture... the mistake in your code is,

<p><img id="main-image" src="images/photo3.jpg" alt=""></p> // from html var imageNode = $("main_image"); // from javascript

id's don't match, its a simple typo, main_image (never equals) main-image

either use <p><img id="main-image" src="images/photo3.jpg" alt=""></p> // from html var imageNode = $("main-image"); // from javascript

or

<p><img id="main_image" src="images/photo3.jpg" alt=""></p> // from html var imageNode = $("main_image"); // from javascript

Now to understand such typos, pay attention to your code and variables... happy coding :)

Upvotes: 1

Related Questions