Image Swap

S

Sybil

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
 
L

Lasse Reichstein Nielsen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top