PDA

View Full Version : Image change error?


wickford
08-20-2003, 07:32 PM
I am trying to dynamically change an image, but I get an error message saying document.images.changeit is null or not an object. What am I doing wrong?
imgoff=new Image(55,17);
imgoff.src="IMAGE 1";
imgon=new Image(55,17);
imgon.src="IMAGE 2";

document.images["changeit"].src=imgon.src;<img src="IMAGE 1" width="55" height="17" id="changeit" />Please delete the other post, it was an accident.

marek_mar
08-20-2003, 07:34 PM
Where is the first part of the script located?

cheesebag
08-20-2003, 07:41 PM
<img src="IMAGE 1" width="55" height="17" id="changeit" name="changeit" />

wickford
08-20-2003, 07:50 PM
I get the same error if the script is in the <HEAD> or the <BODY>.

And I tried using name="changeit", id="changeit", and both. All fail for me as well. :confused:

cheesebag
08-20-2003, 07:52 PM
Are you calling this from an event handler? Can't see what you're doing from out here...

IdentityCrisis
08-20-2003, 07:53 PM
Is this image change onMouseOver? or what event is calling the image change?

IdentityCrisis
08-20-2003, 07:58 PM
If it's an image swap you're looking to do... here ya go...

<html>
<script language="JavaScript">
<!--
var IMAGE1 = new Image();
IMAGE1.src = "IMAGE1.gif";
var IMAGE1on = new Image();
IMAGE1on.src = "IMAGE1-on.gif";
var IMAGE2 = new Image();
IMAGE2.src = "IMAGE2.gif";
var IMAGE2on = new Image();
IMAGE2on.src = "IMAGE2-on.gif";


function on(name){
document[name].src = eval(name + "on.src");
}
function off(name){
document[name].src = eval(name + ".src");
}
//-->
</script>
<body>
<a href="#" onMouseOver="on('IMAGE1');" onMouseOut="off('IMAGE1');"><img src="IMAGE1.gif" name="IMAGE1" border="0"></a><br>
<a href="#" onMouseOver="on('IMAGE2');" onMouseOut="off('IMAGE2');"><img src="IMAGE2.gif" name="IMAGE2" border="0"></a>
</body>
</html>


done...

wickford
08-20-2003, 08:00 PM
Never mind, I think I solved the problem. :)

It wasn't an image swap. I was trying to change it X seconds after loading, but putting it in a function seems to solve it.