Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

JavaScript Rollover Images not working

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
Deanna Andru
Asked:
Deanna Andru
  • 2
1 Solution
 
GaryCommented:
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
 
Slick812Commented:
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
 
Deanna AndruAuthor Commented:
Sorry, like I said, it is "by the book" .
0
 
Deanna AndruAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now