Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Image Swap

Thread Tools

Image Swap

Posts: n/a
Could someone tell me what is wrong with this function?
function imageNext(){

if ( document.part.src == "images/returnAdapter.gif"){
document.part.src = "images/valveIntake.gif";
document.getElementById("c").style.color = "black";
document.getElementById("d").style.color = "red";

The image name attribute is "part". But the image is not changing.

Thanks, Sybil
Reply With Quote
Lasse Reichstein Nielsen
Posts: n/a
      08-26-2003 Removed) (Sybil) writes:

> Could someone tell me what is wrong with this function?

In which browser?

> function imageNext(){

> if ( document.part.src == "images/returnAdapter.gif"){

The assumption that "document.part" refers to the element with name
"part", doesn't hold in all browsers. A safer way of referencing the
element is

You compare the value of the src property to a releative path.
Most browsers (including IE6) changes to property to the absolute path.
E.g., if I create an image as:
<img src="../../PicA.png" id="foo">
and then read the src value again with javascript (in IE 6)
the result is
That means that your comparison will alway fail.
You should test that the src *ends* with your string, e.g., with a
regular expression:

if (/images\/returnAdapter.gid$/.test(document.images['part'].src) {

> document.part.src = "images/valveIntake.gif";

Again, "document.imagesŲ is safer.

> document.getElementById("c").style.color = "black";
> document.getElementById("d").style.color = "red";
> }

A "}" is missing here (just to be pedantic

Lasse Reichstein Nielsen - (E-Mail Removed)
Art D'HTML: <URL:>
'Faith without judgement merely degrades the spirit divine.'
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
next / previous image swap WITH larger image popup Clay Javascript 1 09-28-2006 09:07 PM
What swap is called when using std::swap? Niels Dekker (no reply address) C++ 4 07-20-2006 08:44 PM
Allow multiple image swap onmouseover hadAnet ASP .Net 2 12-28-2005 06:56 PM
Mouse-over an image -> play video instead of swap image? Phillip Roncoroni HTML 6 04-06-2004 06:57 AM
swap image Glenn D HTML 1 03-07-2004 08:09 AM