469,282 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 2545
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
document.images['part']

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)
document.all.foo.src
the result is
"file:///D:/Home/lrn/html/PicA.png"
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 :)

/L
--
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 this site, you agree to our Privacy Policy and Terms of Use.