Solved

JavaScript Rollover Images not working

Posted on 2014-10-15
4
196 Views
Last Modified: 2014-10-26
Hi,

For a Javascript assignment, I am trying to make some rollover images and have followed the book tutorial to the letter and cannot get this code to roll on mouseover.  Can you please assist?

I can provide the code, it includes an htm, a .js and .css files.  Currently the .js file is:
function addEvent(object, evName, fnName, cap) {
    if (object.attachEvent)
    	object.attachEvent("on" + evName, fnName);
    else if (object.addEventListener)
    	object.addEventListener(evName, fnName, cap);
    }
addEvent(window, "load", setupSlideshow, false);

function setupSlideshow() {
    var slides = new Array();
	//populate array of slide images
    for (var i = 0; i < document.images.length; i++) {
    	var thumb = document.images;
    if (thumb.className == "slide" && thumb.parentNode.tagName == "A") {
    	slides.push(thumb);
    }
}    
		for (var i = 0; i < slides.length; i++) {
	// create a rollover for each slide.
    createRollover(slide[i]);
    } 
	
 }
function createRollover(thumb) {
    thumb.out = new Image();
    thumb.over = new Image();
    thumb.out.src = thumb.src;
    thumb.over.src = thumb.src.replace(/_thumb/, "_over");
	
    thumb.onmouseout = function () {
    thumb.src = thumb.out.src;
    }
    thumb.onmouseover = function () {
    thumb.src = thumb.over.src;
    }   
}
function createGallery() {
    var galleryBox = document.createElement("div");
    galleryBox.id = "galleryBox";
	//insert a high-resolution slide
	var slide = document.createElement("img");
	slide.id = "gallerySlide";
    slide.src = "slides01.jpg";
    galleryBox.appendChild(slide);
	document.body.appendChild(galleryBox);
    }

Open in new window

0
Comment
Question by:Deanna Andru
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
We need the full markup (html, css) not just a bit of js

But if this is a homework assignment do no expect a full answer but a clue as to what is wrong.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
Comment Utility
greetings  Deanna Andru, , there are several MANY things Wrong with the bad code work you have in your javascript.

the first thing that jumps out at me is you do NOT know how to deal with a JS DOM element Container as your -
     var thumb = document.images;
the thumb variable is NOT an element, it is a collection of elements, as an ARRAY of DOM images. . . so this test -
       thumb.className == "slide"
can NEVER work, since the .className will always be "undefined" as it does NOT exist. You MUST use the Array access like this -
      thumb[j].className == "slide"

= = = = = = = = = =
next, this line -
      thumb.out = new Image();
makes NO Sense at all to me, you are placing a Second image into a property of the first image, This can not be correct especially since the way you use this, is NEVER as an image, like this line -
      thumb.src = thumb.out.src;
if you only need the web addy for the image, DO not create an image, just save the web addy like -
     thumb.out = thumb.src;
     thumb.over = thumb.src.replace(/_thumb/, "_over");

you need to develop and debug ONE operation (function) at a time, and then move on to the next After you get that to work.
0
 

Author Comment

by:Deanna Andru
Comment Utility
Sorry, like I said, it is "by the book" .
0
 

Author Closing Comment

by:Deanna Andru
Comment Utility
I was able to get the code to work, but comment about bad coding unnecessary, doing this in class and "by the book" - likely to be outdated, yes.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now