Solved

JavaScript Rollover Images not working

Posted on 2014-10-15
4
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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