English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

//just this part below is a problem
sector1red = new Image();
sector1red.src = "sector1red.bmp";
if (speed == 3) {
document.getElementById("sector1").src = sector1red;
}




when speed hits 3, i want "sector1red.bmp" to replace "sector1white.bmp" in the div tag.
is this the right way to display a picture there or what is wrong?

2007-07-02 01:47:19 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

sorry, here is the correction for the get Element method
document.getElementById ("sector1").src = sector1red;

2007-07-02 01:48:38 · update #1

3 answers

First, to reference an image tag you need to assign an id attribute to the image tag and not the div tag that contains the image tag.

So your image tag should be:



and you should take out id="sector1" from the div tag. (As there cannot be multiple id's with same name on one page)


Second, to switch the image you need to say:

document.getElementById( "sector1" ).src = sector1red.src;

As previous responder have said, you can also set the src to a path where the image is in, say:

document.getElementById( "sector1" ).src = "sector1red.bmp";

or if the image is in the /images/ folder:

document.getElementById( "sector1" ).src = "/images/sector1red.bmp";

should also work fine.

But since you have preloaded the image already (declaring an image variable and setting it's src like you did above makes the page download the image to the temporary folder when the page loads and makes it available locally when requested), it makes more sense to go with the first way -> document.getElementById( "sector1" ).src = sector1red.src;

hope this helps and good luck

2007-07-02 09:18:45 · answer #1 · answered by jk 1 · 0 0

No here is one more thing to do properly:
as the tag should go img id=sector1 src...

And .bmp should be here in the changing code.

In simpler words: you got the idea right, but DOM not structured properly.

2007-07-02 09:02:04 · answer #2 · answered by Andy T 7 · 1 0

It looks like you are trying to assign an image to the src id.
Change:
document. getElementById("sector1"). src = sector1red;
to:
document. getElementById("sector1"). src = "sector1red.bmp";
or:
document. getElementById("sector1") = sector1red;

I'm not an expert on javascript by any means, but I do believe this is your problem.

2007-07-02 08:55:45 · answer #3 · answered by Thee John Galt 3 · 0 0

fedest.com, questions and answers