Solved

JavaScript Rollover Images not working

Posted on 2014-10-15
4
199 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
ID: 40383591
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
ID: 40384621
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
ID: 40405462
Sorry, like I said, it is "by the book" .
0
 

Author Closing Comment

by:Deanna Andru
ID: 40405471
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

831 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