...

View Full Version : id instead of name



Jero
08-20-2004, 03:20 PM
Hello. I'd like to make a valid XHTML site, but it uses a mouseover. Because of that I have to give some images the name atribute, but that isn't valid. The atribute id is, but it doesn't work with the javascript. This is my code:


<script type="text/javaScript">
image1 = new Image();
image1.src = "images/over2.gif";

image2 = new Image();
image2.src = "images/producten2.gif";

image3 = new Image();
image3.src = "images/bestellen2.gif";

image4 = new Image();
image4.src = "images/contact2.gif";

<!-- the mouseover -->

<a href="over.html" onmouseover="image1.src='images/over2.gif'" onmouseout="image1.src='images/over.gif'"><img name="image1" src="images/over.gif" width="96" height="44" alt="" /></a>
</script>

Willy Duitt
08-20-2004, 03:49 PM
Then use document.getElementById('image1')

Besides, the method you are using to call the image name is IE proprietary anyway. Thus it would not work for any other browser anyway....

.....Willy

Jero
08-20-2004, 04:11 PM
Oh... I got this code from some website because I have no javascript knowledge. Is there kind of code that supports all browsers?

Thanks.

jamescover
08-20-2004, 04:55 PM
Besides, the method you are using to call the image name is IE proprietary anyway. Thus it would not work for any other browser anyway....

Glenn says this too. What browsers does it not work in? In addition to IE, Mozilla Firefox also supports this syntax. It may be considered a quirk, but it does support it.



-james

Roy Sinclair
08-20-2004, 05:01 PM
Oh... I got this code from some website because I have no javascript knowledge. Is there kind of code that supports all browsers?

Thanks.

Yes, there is. The images on a page are all loaded into a global array named "images" which can be referred to using either the index number (not a good idea) or by the ID. So another way to find the object for those images is:

document.images["id of the image"]

That said, the suggestion by Willy will work in all current browsers, only antiques like IE 4 and Netscape 4 don't recognize the getElementById function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum