471,573 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

Image Swap

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
Jul 20 '05 #1
1 2575
sd*@bdgrp.com (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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.