Solved

JavaScript Rollover Images not working

Posted on 2014-10-15
4
198 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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now