Click to See Complete Forum and Search --> : changing an image


pelegk1
05-28-2003, 05:59 AM
i have an image in my page :
<img src="pic_off"></img>
and i want by code to swap this image to another image
how do i do that?

Nevermore
05-28-2003, 06:04 AM
I assume you mean when the mouse goes over it.
You can either do this in JavaScript or CSS. Which are you more familiar with?

pelegk1
05-28-2003, 07:41 AM
i have found how to do it
but instead of telling me every time to use css (what is not good for what i need) write down some code to show how to swap image using code

Nevermore
05-28-2003, 08:45 AM
Chill!

Swapping images in CSS isn't good anyway - it isn't very compatible at all.

Instead you should probably use JavaScript. As per your request, here's some code:



<a onmouseover="javascript:document.getElementById('image1').src='newimage.gif'" onmouseout="javascript:document.getElementById('image1').src='oldimage.gif'"><img alt="cyclops_angry name="image1" id="image1" (1K)" src="oldimage.gif" height="45" width="45" /></a>

pelegk1
05-29-2003, 02:42 AM
:)

Charles
05-29-2003, 05:31 AM
Originally posted by cijori
Swapping images in CSS isn't good anyway - it isn't very compatible at all. Considering that about 13% of users are not using JavaScript (http://www.thecounter.com/stats/2003/May/javas.php) and that a statistically insignificant number of users use browsers that do not understand CSS (http://www.thecounter.com/stats/2003/May/browser.php) I'd say that you are wrong. And I'd point out that your JavaScript is wrong to, it's just that browsers are recovering well from the error.

Nevermore
05-29-2003, 06:48 AM
I thought IE didn't like using CSS2 to swap images? And could you tell me what is wrong with the JavaScript, so I don't make the same mistake again?

Charles
05-29-2003, 07:00 AM
You are employing the "javascript:" pseudo protocol that is often used with the "href" attribute of an A element. The value of an event handler ought to be pure script and before using one, you ought to have specified the language using something like the following in the ddocument head.

<meta name="Content-Script-Type" content="text/javascript">

Nevermore
05-29-2003, 07:02 AM
Thankyou! I will aspire to do better in future.